pointer-arithmetic

subtracting two addresses giving wrong output

∥☆過路亽.° 提交于 2020-01-14 06:00:09
问题 int main() { int x = 4; int *p = &x; int *k = p++; int r = p - k; printf("%d %d %d", p,k,p-k); getch(); } Output: 2752116 2752112 1 Why not 4 ? And also I can't use p+k or any other operator except - (subtraction). 回答1: First of all, you MUST use correct argument type for the supplied format specifier, supplying mismatched type of arguments causes undefined behavior. You must use %p format specifier and cast the argument to void * to print address (pointers) To print the result of a pointer

Calculate array length via pointer arithmetic

让人想犯罪 __ 提交于 2020-01-13 08:30:33
问题 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

Why does i[arr] work as well as arr[i] in C with larger data types?

拈花ヽ惹草 提交于 2020-01-10 01:35:39
问题 It's fairly common knowledge that if you access an element of an array as arr[i] in C that you can also access the element as i[arr] , because these just boil down to *(arr + i) and addition is commutative. My question is why this works for data types larger than char , because sizeof(char) is 1, and to me this should advance the pointer by just one char. Perhaps this example makes it clearer: #include <string.h> #include <stdio.h> struct large { char data[1024]; }; int main( int argc, char *

Is it guaranteed that array elements in C will be stored consecutively, with no padding?

╄→尐↘猪︶ㄣ 提交于 2020-01-04 02:02:26
问题 In other words: is it guaranteed that if I have an array allocated this way: void *arr = calloc(nmemb, sizeof(some_type)) Then elta , eltb , eltc will all point to the same location in memory, which will be the second element of type some_type of this array? some_type *elta = &((some_type*)arr)[1]; some_type *eltb = ((some_type*)arr)+1; some_type *eltc = (char*)arr+sizeof(some_type); The reason I’m asking this is because I’m trying to do a “container” in C, and if this doesn’t hold then I’m

How do I apply a structure offset?

醉酒当歌 提交于 2020-01-03 18:43:12
问题 I have a structure typedef struct foo { int lengthOfArray1; int lengthOfArray2; int* array1; int* array2; } foo; I need to allocate enough memory for the entire structure and its array's contents. So assuming each array had a length of 5... foo* bar = (foo*)malloc(sizeof(foo) + (sizeof(int) * 5) + (sizeof(int) * 5)); I now have to point array1 and array2 to the correct location in that allocated buffer: bar->array1 = (int*)(&bar->lengthOfArray2 + sizeof(int)); bar->array2 = (int*)(bar->array1

create my own memset function in c

北城余情 提交于 2020-01-03 03:06:07
问题 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

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

℡╲_俬逩灬. 提交于 2020-01-02 14:31:53
问题 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()

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

一个人想着一个人 提交于 2020-01-02 14:30:11
问题 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()

warning: pointer of type ‘void *’ used in arithmetic

你说的曾经没有我的故事 提交于 2020-01-02 00:53:15
问题 I am writing and reading registers from a memory map, like this: //READ return *((volatile uint32_t *) ( map + offset )); //WRITE *((volatile uint32_t *) ( map + offset )) = value; However the compiler gives me warnings like this: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith] How can I change my code to remove the warnings? I am using C++ and Linux. 回答1: Since void* is a pointer to an unknown type you can't do pointer arithmetic on it, as the compiler wouldn't know

difference between *y++ and ++*y?

自作多情 提交于 2020-01-01 11:45:38
问题 I'm confused in how this code will get executed. Suppose we have int x=30,*y,*z; y=&x; what is the difference between *y++ and ++*y? and also what will be the output of this program? #include<stdio.h> int main(){ int x=30,*y,*z; y=&x; z=y; *y++=*z++; x++; printf("%d %d %d ",x,y,z); return 0; } 回答1: The expression x = *y++ is in effects same as: x = *y; y = y + 1; And if expression is just *y++; (without assignment) then its nothing but same as y++; , that is y start pointing to next location