pointer-arithmetic

Array Assignments in C Using Pointer Arithmetic

廉价感情. 提交于 2019-12-11 04:42:36
问题 How can I change the value in an array when I access a particular element using pointer arithmetic? #include <stdio.h> int main() { int a[3] = {1, 1, 1}, b[3] = {2, 2, 2}; a++ = b++; // How can I get this to work so a[1] = b[1]? return 0; } 回答1: Arrays are not pointers. Repeat this three times; arrays are not pointers . You cannot increment an array, it is not an assignable value (i.e., you cannot mutate it). You can of course index into it to get a value back: a[1] = b[1]; Secondly, your

Array memory Allocation doesn't work

狂风中的少年 提交于 2019-12-11 04:21:02
问题 I have the next classes: class A { }; class B : public A { int num; }; in my main I have: int main() { A* vec; // A is a class with pure virtual functions vec = new B[2]; // want to create a vector of B } vec[0] is defined correctly, but vec[1] is NULL. why didn't it allocate me a fit memory? I don't want to change the lines of the main. just make it working. (I know I can change the main into: B* vec = new B[2] but I don't want) any help appreciated! 回答1: You cannot treat arrays

-Wpedantic wrong type argument to increment after casting

我与影子孤独终老i 提交于 2019-12-11 03:59:39
问题 I have a code like while (n--) { *((char*)dest++) = *((char*)src++); } where dest and src are void pointers and n a size. The goal is to re-implement a memcpy function. When compiling this code with gcc, everything works great, but when I add the -Wpedantic flag I have four warnings "wrong type argument to increment". Google tells me that it happens when trying to use arithmetic on void pointers, because gcc treats void type as being a 1 byte type in this case, but legacy compilers shoud not.

The new IntPtr.Add method - am I missing the point of the int?

狂风中的少年 提交于 2019-12-10 23:43:28
问题 Starting from FW 4.0, the IntPtr structure has the Add method: public static IntPtr Add( IntPtr pointer, int offset ) Which is great, as it's supposed to address all those questions on IntPtr math we have had (1, 2, probably more). But why is the offset int ? Must it not be IntPtr ? I can easily imagine offsetting a 64-bit pointer by a value which is beyond the int range. For instance, consider Marshal.OffsetOf: public static IntPtr OffsetOf( Type t, string fieldName ) It returns an IntPtr as

When is pointer subtraction undefined in C?

血红的双手。 提交于 2019-12-10 12:39:31
问题 char *buf = malloc(bufsize) char *ptr = buf; … while(condition) { ptrdiff_t offset = ptr - buf; // <========== THIS LINE // offset will never be negative because we only ever *increase* ptr if ((size_t)offset > bufsize) { // we need more room bufsize += 128; buf = realloc(buf, bufsize); ptr = buf + offset; // buf might be in a completely new location } *ptr++ = … // write this byte } Is this valid or undefined ? I would have assumed that it's valid, but I read something about it being

How to wisely interpret this compiler warning?

岁酱吖の 提交于 2019-12-10 04:35:30
问题 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

Reading signed char using %u

為{幸葍}努か 提交于 2019-12-10 03:10:46
问题 #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

Issues with Pointer Arithmetic - Trying to tokenize input String

回眸只為那壹抹淺笑 提交于 2019-12-08 11:24:22
问题 Currently I am working on a program that allows a user to enter a string that is then tokenized, then the tokens are printed to the screen by using an array of pointers. It is "supposed" to do this by calling my tokenize function which reads the input string until the first separator ( ' ', ',', '.', '?', '!'). It then changes that separator in my string to a NULL char. It then should return a pointer to the next character in my string. In main after the string has been input, it should keep

Is there a defined way to do pointer subtraction in C11?

拈花ヽ惹草 提交于 2019-12-08 05:45:17
问题 Is there a way to subtract one pointer from another in C11 and have the result be always defined? The standard says the behavior is undefined if the result is not representable as type ptrdiff_t. I am open to a solution relying on static assertions that are expected to pass on a reasonable implementation in a modern general purpose 32 or 64 bit environment. I would like to avoid solutions that rely on any sort of runtime checks. If the pointed to type has size greater than 1, I can static

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

谁说我不能喝 提交于 2019-12-07 13:02:32
问题 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