Question:
SAVE ME!!Henni,Ratchetr,Jonathan,Oops,BADLY need ur help in C:Pointers to arrays,arrays of pointers to arrays!?
electric
2012-08-30 08:05:05 UTC
Please bail me out this time.I BEG YOU!!I won't bug you with questions this long again.I've really hit a major bottleneck in C due to pointers.I badly need your answers to this crucial one.I am not that articulate & so it took me half a day to phrase the following question to ask what I intend to ask.So please bear with me and kindly answer it.I shall be EVER grateful to you.

It flows from the following question I had posted week back and which Contributor Henni answered:

http://answers.yahoo.com/question/index;_ylt=Amz6Je7QyeK5fyTuVqE3bgfty6IX;_ylv=3?qid=20120731063310AAZtM5j

I have divided my question into bits for clarity.It will be really nice & helpful of you if you can answer them in the same numbered order given the fact that I am not that good in programming yet.If you are too busy,I'll extend the duration of this question & wait till you get the time to answer.(My email is sheerfish@yahoo.com).THANKS.

So here's my question.

1)Going by Henni's answer to my older question linked above,if we declare a pointer to an integer array as "int (*ptr)[]" then string being a character array, why don't we declare a string pointer as "char (*ptr)[]"?Since this itself leads to "sub-questions", I have posted those in a different (short) question

http://answers.yahoo.com/question/index?qid=20120830080202AA2VbAC

2)IMPORTANT: If "int *(*ptr)[]" is a pointer to an array of integer pointers , what is **ptr[] then?Shouldn't it follow from Henni's definitions of "int (*ptr)[]" and "int *ptr[]" in the topmost linked question that "int **ptr[]" should be a "pointer to an array of integer pointers" and "int *(*ptr)[]" should be "a pointer to a pointer to an integer array"?Especially,HOW WOULD YOU WRITE A "POINTER TO A POINTER TO AN INTEGER ARRAY" THEN?Please give the reasons for your answer to this.

3)IMPORTANT:How do we declare an array of pointers to integer arrays(Is it as similar as "char *argv[]" being used to declare an array of pointers to character arrays(ie strings))?(Look at it in the context of how we declare an array of pointers to character arrays(strings) in "char *argv[]".If "char *argv[]" can mean an array of pointers to arrays of characters(strings),WHY CAN'T "int *ptr[]" MEAN "AN ARRAY OF POINTERS TO INTEGER ARRAYS" INSTEAD OF "AN ARRAY OF INTEGER POINTERS"?To repeat the main question, how do we declare an array of pointers to integer arrays, ie an array of pointers to a 2D integer array"?

4)If an array of pointers to characters is the same as an array of pointers to strings(from the case of "char *argv[]"),thereby meaning a pointer to a character is the same as a pointer to an array of character(string) (we declare it "char *ptr" for both character and string) then does it also mean that array of pointers to integers (ie. "int *ptr[]") is same as array of pointers to integer arrays, THEREBY meaning a pointer to an integer is the same as a pointer to an integer array? But that's clearly not so as we have different ways to declare an integer pointer and a pointer to an integer arrray("int *ptr" and "int (*ptr)[]" respectively)!!(Again refer to Henni's answer in the topmost linked question).And on the reverse, if declaration of pointer to an integer is different from declaration of pointer to an integer array,meaning an array of integer pointers can't point to integer arrays,then how come an array of character pointers point to character arrays(or strings) in "char *argv[]"??AAAGHH, GOD HELP ME!!

5)Now let me ask this rigorosly--Is the declaration of an array of pointers to strings same as declaration of an array of pointers to characters?Suppose I want to declare an array of pointers to strings and an array of pointers to characters.Here I define "array of pointers to strings" as "array of pointers to arrays of characters(.ie strings are arrays of characters after all) and I define "array of pointers to characters" as "array of pointers to single characters" as different from "arrays of characters or strings".So will the declaration for both be same as following?
char *ptr[];
Though I feel it's so , I want to confirm from you.I feel so because in a C main function, we write "int main(int argc,char *argv[])" and we say argv is an array of pointers to strings.If that's so please explain the following as it's directly related to the above:

a)Assuming an array of pointers to strings is same as array of pointers to strings, does it mean that the compiler determine whether the pointer wants to point to a single character or a string depending on context? Consider the following :
--If I pass an array element from "array
Three answers:
2012-08-30 20:24:04 UTC
1) There's no 'string type' in C. ``char (*ptr)[]'' has the type ``pointer to array (with unknown size) of char''.



2)

> "int **ptr[]" should be a "pointer to an array of integer pointers

No. That is ``array of pointer to pointer to int''.



> "int *(*ptr)[]" should be "a pointer to a pointer to an integer array"?"

No. Pointer to array of pointer to int.



> Especially,HOW WOULD YOU WRITE A "POINTER TO A POINTER TO AN INTEGER ARRAY" THEN?

int (**ptr)[];



3) ``char *[]'' means ``char **'' in function prototypes. It's a special case. An array of pointer to array of int would look like this: int (*[])[];



Note that char **argv has absolutely nothing to do with arrays. It points into arrays, but its type is ``pointer to pointer to char''.



> how do we declare an array of pointers to integer arrays, ie an array of pointers to a 2D integer array"?



An array of pointer to array of array of int is: int (*[])[][];



4)



> If an array of pointers to characters is the same as an array of pointers to strings



There are no pointers to strings or pointers to characters. There are pointers to char. There is no string type.



...



Since the rest of your questions assume that C has a 'string' type, I can't really answer them. You need a C reference like K&R2.



edit:





1) Sequences of chars make up strings, whereas sequences of arrays of chars don't. That's why you use ``char *'' when handling them instead of ``char (*)[]''.



2) arr yields a value of type char *. ptr stores a value of type char (*)[].



3)

> In "char *argv[]" if argv is an array of pointers to strings(=character arrays),



It isn't. argv is a pointer to pointer to char because it's defined in a parameter list.



At function prototype scope: char *argv[10] defines argv as a pointer to pointer to char.



At file scope/block scope: char *argv[10] defines argv as an array 10 of pointer to char.



The reason for this difference is because you can't pass arrays to functions in C.
?
2012-08-31 03:18:33 UTC
I think your primary point of confusion is this:



"how come an array of character pointers point to character arrays(or strings) in "char *argv[]"??"



and you're almost answering it yourself:



"does it mean that the compiler determine whether the pointer wants to point to a single character or a string depending on context? "



The answer is no. The compiler is not aware of "strings" at all. From compiler's point of view, char *argv[] is an array of pointers to single characters.



The functions that work with strings in C, such as strlen(), take pointers to a single characters as their arguments. However, these functions also specify a precondition: the pointed-to character must be an element of a character array, and there must be a null character somewhere in that character array, either at the pointed-to location or later. The precondition is not enforced by the compiler, it is the programmer's responsibility.
Paul
2012-08-30 15:45:38 UTC
There's a lot here but it's not a bad question.



My recommendation is to ask this on Stack Overflow where you will get more intelligent answers then on here. Seriously, ask there and you will get a good answer.

http://stackoverflow.com/



Definitely read this:

http://c-faq.com/aryptr/

To understand the difference between an array and a pointer.





I'll start us off by making a correction that is kind of important.

An array must know it's size at compile time. You can not have an ordinary array whose size is not known at compile time.



So the following are all fine because size is known at compile time

int a[2]; // size is 2

char b[] = "hello"; // This is EXACTLY the same as saying char b[6] = "hello". size is 6.

char c[] = "yo"; // This is EXACTLY the same as saying char c[3] = "yo". size is 3.

char d[50] = "Waddup"; // size is 50

char (*e)[2]; // size is 2

char (**f)[5]; // size is 5

char *g[2]; // This is a pointer to an array of size 2



The following are NOT okay because size is not known at compile time

int a[]; // What would size be?

char b[]; // ditto

char (*c)[]; // ditto

char (*d)[]; // ditto

int *e[]; // This is a pointer to an array but what is the array's size?





I know your question asks a lot more but this is a start. I might add to this answer later.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...