[Solved] Why can’t we store addresses in normal int variables? and by assigning i dont want it to point anywhere.

why we need pointer variables… No, we don’t need pointer variables until… we need one. In other words, there are many cases (or algorithms) which don’t need pointers. But soon or later you realize that pointers are needed. Well, pointers contain values, which are different from integers, different from floating point numbers and so on. … Read more

[Solved] confusing notation in C++ (OMNeT++)

Let us assume that msg->dup() returned void * — that is, a pointer to void, which means a pointer whose type the compiler doesn’t track. But you may know, e.g. because of documentation on that function, or because certain preconditions have been met, that msg->dup() will return a pointer to CMessage. Before you can use … Read more

[Solved] Null terminated C character arrays

First, “sample” is called a string literal. It declares a const char array terminated with a null character. Let us go on: char arr[]=”sample”; The right hand part in a const char array of size 7 (6 characters and a ‘\0’. The dimension of arr is deduced from its initialization and is also 7. The … Read more

[Solved] How can I compare pointers in Go?

Is it guaranteed that the order of the pointers remains the same? (For example, I can imagine some GC trickery which also reorders the data.) In answer to this part of your question, it doesn’t look like it. See pkg unsafe linked below. If you want the memory address of a pointer, try the unsafe … Read more

[Solved] Unexpected output of printf for a string in C

None of those two cases are right. Case 1 only worked because you got lucky, probably by giving a short string as input. Try something like “bfjabfabjkbfjkasjkvasjkvjksbkjafbskjbfakbsjfbjasbfjasbfkjabsjfkbaksbfjasbfkja” and you’ll suffer a seg fault, most likely. You should have a block of memory associated with str, either on the stack by declaring an array for it … Read more

[Solved] why the array is static [closed]

The function isn’t the pointer. The return value is int * since it returns an array of int. You need a pointer to access an array. If it’s not a pointer, then you are expecting a single int variable. If it’s not static, then the array will be deallocated and gone when the function reached … Read more

[Solved] Cast increment and assign in C

[How can I] De-reference and assign a value to a pointer and increment the pointer by (uint64_t) bytes in one line[?] You are trying to do too many things with a single expression. You should break it up. Moreover, “one line” is more or less arbitrary. I could take your one-liner and spread it out … Read more

[Solved] Can any one please help me what wrong with this code? [closed]

Ok, you dynamically allocate 10 bytes. Then you make the “name” variable (a char pointer) point to that dynamically allocated block. Then you make a char pointer which points to the same dynamically allocated block that “name” points to. Then you “free(name)” which deallocates the block that “tmp_name” also points to. “name” and “tmp_name” both … Read more

[Solved] Why no error is thrown in compilation? [closed]

Can this be a way to initialize an array without declaring its size? No. All int *a; does, if defining a pointer to int, which, as not being initialised, points somewhere, “nowhere”, to “invalid” memory. Any invocation of the []-operator on a, without beforehand having made a point to any valid memory (as for example … Read more

[Solved] C runtime error (undefined behaviour) for performing ++*(p++) on string literal char *p = “abcd”

char *p=”abcd”; “abcd” is a string literal and string literals are unmodifiable in C. Attempting to modify a string literal invokes undefined behavior. Use a modifiable array initialized by a string literal to fix your issue: char p[] =”abcd”; 7 solved C runtime error (undefined behaviour) for performing ++*(p++) on string literal char *p = … Read more

[Solved] C Pointer of another pointer

In the printf call you’re casting the value of B to char *, but that’s not what you assigned to it. You assigned &A which has type char **. You need to either assign A to B: void *B = A; printf(“%p — %p — %s — %s”, (void *)&A, (void *)B, A, (char *) … Read more