Question:
In C, what's wrong with "char name[20]="Seattle",(*ctr)[]=name;"?Is it wrong way to declare string pointer?Why?
electric
2012-08-30 08:02:03 UTC
Please consider the following little program and clear the 2 confusions I encountered due to it thereafter.

#include

int main()
{

char name[20]="Seattle",(*ctr)[]=name; //Shows warning
*ptr=name; //works fine as expected

printf("%s,%s",ptr,ctr);

}

OUTPUT: Seattle,Seattle


1)Why does "(*ctr)[]=name" show following warning? "warning: initialization from incompatible pointer type|" but there is no warning when I write "(*ctr)[]=&name". Don't we write the string/array name on the right side of "=" operator when assigning base address of strings/array?Why is "*ctr)[]=&name" working fine even though we are using "&" before an array name?Isn't "&" operator used only before single variables, not arrays?

2)As string is an array of characters, why dont' we declare the string pointer as "char (*ctr)[]" nstead of "char *ctr"?Going by the answer of a top contributor in the following question

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

the correct declaration for a pointer to an array of characters (eg string) should be "char (*ctr)[];" where "ctr" is the pointer.Does it mean a "pointer to an array of characters(string)" is the same as "a pointer to a character"?God, I am really confused about it.
Three answers:
Sathish
2012-08-30 08:26:39 UTC
"(*ctr)[]=name"// shows warning



what you are trying to do is create an array of character pointers....each pointer can act as an individual array. which means your code "(*ctr)[]" is a two dimensional array .....for single dimensional array "name" can be pointed by the pointer "*ptr" since as mentioned earlier pointers can be used as a single dimensional array( are of similar type).however for pointing to a two dimensional array you have to specify by its address as "name" is a single dimension array .hence "(*ctr)[]=&name". shows no warning
shiley
2016-12-12 00:19:43 UTC
I consistently puzzled approximately that too. in specific circumstances a attractiveness has a meaning in yet another language and that seems to close those human beings up. I have been given somebody on Takeo Spikes the different day, when you consider that Takeo is jap for "reliable like bamboo." i think of maximum persons say a attractiveness isn't real in the event that they do no longer look to be conscious of the call. i will say I even have matters with human beings in basic terms spelling real names incorrect and then calling it a sparkling call. Sorry, yet Anfernee is in basic terms somebody spelling Anthony incorrect. the only different element is that I draw back in specific circumstances whilst somebody names their newborn Qwonisha or Lefawntay or some thing. i do no longer possibly care...that's Q and Fawn to me...yet stable success getting a job, newborn.
varun
2012-08-30 08:12:04 UTC
#include



int main()

{



char name[20]="Seattle".(*ctr)[]=name; //Shows warning

*ptr=name; //works fine as expected



printf("%s,%s",ptr,ctr);



}


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