Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How store address of an array[] in a variable

This seems like a silly question. I have an array of chars and want to store the address of the array in another variable, but can’t seem to declare the correct type for the array address (I’m using gcc):


IN:

int main(void){
  char cha[] = "abcde";
  char **arrayAddress = &cha;
}

OUT:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:25: warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[6]’ [-Wincompatible-pointer-types]
    3 |   char **arrayAddress = &cha;
      |                         ^

This is expected, I have read elsewhere that the type of cha should be char(*)[6]. But when I try to declare arrayAddress with this type, my program fails:


IN:

int main(void){
  char cha[] = "abcde";
  char (*)[6]arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:10: error: expected identifier or ‘(’ before ‘)’ token
    3 |   char (*)[6]arrayAddress = &cha;
      |          ^
make: *** [<builtin>: arrayaddress] Error 1
       ^

How do I define arrayAddress correctly?

>Solution :

It is written:

char (*arrayAddress)[6] = &cha;

Notice that the name of variable gets tucked in middle of the expression.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading