C arrays

Programming, for all ages and all languages.
Post Reply
LongHorn

C arrays

Post by LongHorn »

I get confused with arrays when it is taking more than two dimensions and same way with pointers. Most of questions are always about this? I so make mistakes some time. So could you explain how it's translated into instructions? I don't want to disassemble and figure out the whole day. So somebody will you do it for me?
for eg

Code: Select all

char students[][]={"Hello","World"};
char **p;
p=students;
print(*p++);
Something like that.
zloba

Re:C arrays

Post by zloba »

Code: Select all

char students[][]={"Hello","World"};
a literal string, like "Hello", is actually of type char* (or const char*), pointing to some location you don't care about (and shouldn't try to modify).

if you write: char student[]="Hello" - that creates a local array filled with these chars, size depends on the string size.
you could also write const char* student = "Hello", if you don't need a local array to modify.

now, adding another array level to that makes it an array of these:
char students[] [].

actually, that's a lie. that won't compile, because the first [] means an array of indeterminate size, to be determined by the data. you can't have an array of items of unknown size.

Code: Select all

$ gcc foo.c
foo.c: In function ?main?:
foo.c:5: error: array type has incomplete element type
what you could do is make an array of char* pointers, that works:

Code: Select all

char* students []={"Hello","World"};
as for

Code: Select all

print(*p++);
i'm not sure about the priority of operations. i would just avoid such humanly ambigous constructs where meaning isn't immediately clear.
LongHorn

Re:C arrays

Post by LongHorn »

Well something like that. What i really want to know is how arrays and pointers are internally dereferenced. Thanks for reply zloba
zloba

Re:C arrays

Post by zloba »

how arrays and pointers are internally dereferenced
why? are you taking a compiler course? whose "questions"?

you dereference or index an array or pointer - you get a dereferenced object of the item type. how it's done internally is up to the compiler.. say, the offset gets computed into a register, and then used somehow to access the value.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C arrays

Post by Solar »

Declaring [tt]int array[3];[/tt] gives you pointer to three consecutive integer values, called [tt]array[/tt]. You can use either [tt]*(array + 1)[/tt] or [tt]array[1][/tt] to access the middle element.

Declaring [tt]int array_2[5][3];[/tt] gives you a pointer to five consecutive pointers to three consecutive integer values each. You can use [tt]array_2[ 0][/tt] to get the first of these pointers, which has the same type as [tt]array[/tt] above (a pointer to three consecutive integer values). [tt]array_2[ 0][1][/tt] will give you the middle element of that array.

If you are initializing this array like this:

Code: Select all

int array_2[5][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 }, { 13, 14, 15 } };
...the values will be present in memory in just that order, consecutively.

n-dimensional arrays are just an extension of the principle. Always remember that you don't really get an n-dimensional array, but only pointers to pointers to pointers to one-dimensional arrays.

I hope this helps.
Every good solution is obvious once you've found it.
Post Reply