pointer-arithmetic

create my own memset function in c

社会主义新天地 提交于 2019-12-06 23:09:29
here is the prototype: void *memset(void *s, int c, size_t n) first im not sure if I have to return something because when I use the memset i do for example memset(str, 'a', 5); instead of str = memset(str, 'a', 5); here is where I am with my code: void *my_memset(void *b, int c, int len) { int i; i = 0; while(b && len > 0) { b = c; b++; len--; } return(b); } int main() { char *str; str = strdup("hello"); my_memset(str, 'a', 5); printf("%s\n", str); } I dont want to use array in this function, to better understand pointer and memory, so I dont get 2 things: - how to copy the int c into a

What is the difference between pointer to array and pointer to pointer?

假装没事ソ 提交于 2019-12-06 13:41:35
I'm new in programming and learning about pointers in array. I'm a bit confused right now. Have a look at the program below: #include <stdio.h> int fun(); int main() { int num[3][3]={23,32,478,55,0,56,25,13, 80}; printf("%d\n",*(*(num+0)+1)); fun(num); printf("%d\n", *(*(num+0)+1)); *(*(num+0)+0)=23; printf("%d\n",*(*(num+0))); return 0; } int fun(*p) // Compilation error { *(p+0)=0; return 0; } This was the program written in my teacher's notes. Here in the main() function, in the printf() function dereference operator is being used two times because num is pointer to array so first time

Why scanf(“%s”,&str); behaves as scanf(“%s”,str);? [duplicate]

心已入冬 提交于 2019-12-06 03:21:14
This question already has answers here : How come an array's address is equal to its value in C? (6 answers) Closed 3 years ago . Look at the following code: #include <stdio.h> int main() { char str[80]; int n; scanf("%s%n",str,&n); printf("%s\t%d",str,n); putchar('\n'); getchar(); //to remove '\n' scanf("%s%n",&str,&n); printf("%s\t%d",str,n); return 0; } Here is the input and output: abc abc 3 123 123 3 As we know, scanf is a variable parametric function, so its parameters will not be cast when it's called. As a result, parameters must be passed in the type exactly what them should be.

Can you cast a “pointer to a function pointer” to void*

主宰稳场 提交于 2019-12-05 21:53:33
Inspired by comments to my answer here . Is this sequence of steps legal in C standard (C11)? Make an array of function pointers Take a pointer to the first entry and cast that pointer to function pointer to void* Perform pointer arithmetic on that void* Cast it back to pointer to function pointer and dereference it. Or equivalently as code: void foo(void) { ... } void bar(void) { ... } typedef void (*voidfunc)(void); voidfunc array[] = {foo, bar}; // Step 1 void *ptr1 = array; // Step 2 void *ptr2 = (char*)ptr1 + sizeof(voidfunc); // Step 3 voidfunc bar_ptr = *(voidfunc*)ptr2; // Step 4 I

Subtraction between pointers of different type [duplicate]

心不动则不痛 提交于 2019-12-05 21:31:48
This question already has an answer here: Pointer/Address difference [duplicate] 3 answers I'm trying to find the distance in memory between two variables. Specifically I need to find the distance between a char[] array and an int. char data[5]; int a = 0; printf("%p\n%p\n", &data[5], &a); long int distance = &a - &data[5]; printf("%ld\n", distance); When I run my my program without the last two lines I get the proper memory address of the two variables, something like this: 0x7fff5661aac7 0x7fff5661aacc Now I understand, if I'm not wrong, that there are 5 bytes of distance between the two

Pointer arithmetic across subobject boundaries

与世无争的帅哥 提交于 2019-12-05 09:16:04
问题 Does the following code (which performs pointer arithmetic across subobject boundaries) have well-defined behavior for types T for which it compiles (which, in C++11, does not not necessarily have to be POD) or any subset thereof? #include <cassert> #include <cstddef> template<typename T> struct Base { // ensure alignment union { T initial; char begin; }; }; template<typename T, size_t N> struct Derived : public Base<T> { T rest[N - 1]; char end; }; int main() { Derived<float, 10> d; assert(

How to wisely interpret this compiler warning?

浪尽此生 提交于 2019-12-05 07:02:58
When I executed the code of this question , I got this warning: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] printf("P-Q: %d, P: %d, Q: %d", (p - q), p, q); ~^ ~~~~~~~ %ld As a reflex fix, I used %ld to print the subtraction of two pointers. And the compiler agreed. Fortunately, I saw a comment from another user mentioning that %td should be used, since the result type of the subtraction is ptrdiff_t . This answer confirms this claim. Now from GCC's header file of stddef.h , I can see that these types are equivalent in this case: typedef _

Reading signed char using %u

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 04:40:33
#include <stdio.h> int main() { int i,n; int a = 123456789; void *v = &a; unsigned char *c = (unsigned char*)v; for(i=0;i< sizeof a;i++) { printf("%u ",*(c+i)); } char *cc = (char*)v; printf("\n %d", *(cc+1)); char *ccc = (char*)v; printf("\n %u \n", *(ccc+1)); } This program generates the following output on my 32 bit Ubuntu machine. 21 205 91 7 -51 4294967245 First two lines of output I can understand => 1st Line : sequence of storing of bytes in memory. 2nd Line : signed value of the second byte value (2's complement). 3rd Line : why such a large value ? please explain the last line of

Calculate array length via pointer arithmetic

拈花ヽ惹草 提交于 2019-12-05 02:24:36
I was wondering how *(&array + 1) actually works. I saw this as an easy way to calculate the array length and want to understand it properly before using it. I'm not very experienced with pointer arithmetic, but with my understanding &array gives the address of the first element of the array. (&array + 1) would go to end of the array in terms of address. But shouldn't *(&array + 1) give the value, which is at this address. Instead it prints out the address. I would really appreciate your help to get the pointer stuff clear in my head. Here is the simple example I'm working on: int numbers[] =

Pointer arithmetic and arrays: what's really legal?

狂风中的少年 提交于 2019-12-05 01:40:26
问题 Consider the following statements: int *pFarr, *pVarr; int farr[3] = {11,22,33}; int varr[3] = {7,8,9}; pFarr = &(farr[0]); pVarr = varr; At this stage, both pointers are pointing at the start of each respective array address. For *pFarr, we are presently looking at 11 and for *pVarr, 7. Equally, if I request the contents of each array through *farr and *varr, i also get 11 and 7. So far so good. Now, let's try pFarr++ and pVarr++ . Great. We're now looking at 22 and 8, as expected. But now..