Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C, pointers and strings

Name: Anonymous 2011-11-06 5:10

Okay, I get this much:
C does two kinds of memory management: Manual allocation from the heap (malloc, free) and automatic allocation from the stack.

Most functions take char pointer arguments instead of a char array as the string. Additionally, it seems pointers and array share a very odd relationship, so that x[y]=y[x].

What would be a good way to familiarize myself with the concepts of C and its way of dealing with memory? Jumping into the cold water, try to get as far as possible and learning by segfault or is there decent literature on the topic?

Name: Anonymous 2011-11-06 5:18

Read thy K and read thy R.

Name: Anonymous 2011-11-06 5:27

pointers are addresses on a memory. what else is there to learn?

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2011-11-06 6:35

>it seems pointers and array share a very odd relationship, so that x[y]=y[x].
x[y] is Base X + Y (Address of Accessed Datatype*ImpliedDateTypeLength)
Addition is associative so it x+y=y+x and since x[y*datatypelength]=y[x]=[y*datatypelen]+BaseX=
Y (Address of Accessed Datatype*ImpliedDateTypeLength)+Base X

Name: Anonymous 2011-11-06 16:16

>>1
>>4
not confusing enough yet
x[y] is equvialent to *(x+y)

no decent literature will save you from hours of stackfucks, segfaults and stray pointers with no pointees, some things need to be experienced

Name: kodak_gallery_programmer !!RUok9kNlfAMW6vT 2011-11-06 16:21

>>4

x[y] is Base X + Y (Address of Accessed Datatype*ImpliedDateTypeLength)

The standard doesn't impy this and the VAX machine at work shows something different.

Addition is associative so it x+y=y+x and since x[y*datatypelength]=y[x]=[y*datatypelen]+BaseX=
Y (Address of Accessed Datatype*ImpliedDateTypeLength)+Base X

This isn't true signed arithmetic you fucking nigger.

Name: Anonymous 2011-11-06 17:23

>>5
x[y] is equvialent to *(x+y)
That actually makes a lot more sense than the usual obscurities people post. In fact it's a handy mnemonic, and K&R did a good job of explaining it.

Name: Anonymous 2011-11-06 22:22

>>7

real men write 24[a] instead of a[24]

Name: Anonymous 2011-11-07 0:50

>>1
>Most functions take char pointer arguments instead of a char array as the string.
No. Arrays ARE just pointers. They just happen to usually have more than one element to them.

Name: Anonymous 2011-11-07 1:02

>>9
Also note that the array semantics are lost as soon as they array is passed to the function - while sizeof(array) returns the correct size in the function allocating the array (as long as it is stack-allocated), sizeof(array) within a function receiving it as a parameter will return sizeof(int) or whatever size a pointer is on that system.

Name: Anonymous 2011-11-07 5:49

>>9
not exactly, arrays are allocated directly on stack. instead of using a pointer to this data compiler already knows where they are located and there is no need to allocate a memory for address of data. compiler simply reaches to array by adding a constant to stack pointer. for pointers it allocates another memory on stack for address of array and reaches array using that value.

for example   char a[] = "hello world";
  char *b = "hello world";
  printf("a size %d\n%x\n%x\n", sizeof(a), a, &a);
  printf("b size %d\n%x\n%x\n", sizeof(b), b, &b);

output is a size 12
bf4b1f74
bf4b1f74
b size 4
80485f8
bf4b1f70


for example in a's case sizeof a returns 12, which is size of the array allocated for "hello world" but for b it returns 4, which is size of a pointer. another difference is there is no address and content of a because there is no address for a.

also for example b++ is valid while a++ is not for same reasons

Name: Anonymous 2011-11-07 5:59

>>9
No, arrays are not pointers. Arrays are not variables. They can't be assigned to, passed by value nor be returned.

Arrays are names of an automatically or statically allocated block of memory. Using an array, it usually decays to the address of its first element. But the type of an array is not a pointer.

int array[N], *p, (*q)[N];

p = array + 1;
q = &array + 1; // not the same as (int**)

Don't change these.
Name: Email:
Entire Thread Thread List