0

How should I assign "elCorteIngles" memory address to my pointer?

xcode is saying "Array type 'char *[20]' is not assignable"

Thats the code, thanks in advance:

int main(int argc, const char * argv[])
{

@autoreleasepool {

    char elCorteIngles[20] = "Av. Jaime III";
    char *paginasAmarillas[20];

    paginasAmarillas = &elCorteIngles;

    NSLog(@"Según ECI su dirección es           %s", elCorteIngles);
    NSLog(@"Según PagsAmarillas su dirección es %s", *paginasAmarillas);


}
return 0;
}
2
  • What are you trying to accomplish? Why do you need a pointer to an array? Commented May 17, 2014 at 19:08
  • @JoshCaswell my book exercises show a pointer example with int's and I thought an example with "real addresses" may look better. Commented May 17, 2014 at 20:06

2 Answers 2

3

This is how you do it,

  char elCorteIngles[20] = "Av. Jaime III";

  char *paginasAmarillas = NULL;

  paginasAmarillas = elCorteIngles;

Since, elCorteIngles is already an array it points to the first address of the array. So, you will have to assign the first address of elCorteInfles to the paginasAmarillas. Since *paginasAmarillas is the value that the address points, you will have to assign the address of the first location of elCorteIngles contiguous location to the pointers initial pointed address location.

Sign up to request clarification or add additional context in comments.

5 Comments

Now the output is correct, but if I want put more code and update the value of "elCorteingles" using the pointer will not work. (I tested)
What kind of this are you trying to accomplish ? Could you elaborate !
I want a pointer that can update strings of a determined variable, but I not sure how to do it... may be creating some kind of function?
@pedaleo you can update elCorteIngles and access the contents via paginasAmarillas. If it seems not to work for you, you must have done something else wrong and you could post the actual code that is not working, if you can't figure it out.
@Matt McNabb Yes the code is ok. The concept I didn't know is how to re-assign the new "string", the answer was strcopy()
0

The C language does not allow you to directly assign strings to a character array. You may either assign a string at initialization like you did above or by moving characters into the array. The C standard library functions strcpy() and strcat() are typically used to modify the contents of strings.

For example:

char theString[10] = "ab";

Allocates a 10-character array and initializes the first three characters of the string to 'a', 'b', and '\0' because C-strings are NULL-terminated. This is equivalent to:

char theString[10];

theString[0] = 'a';
theString[1] = 'b';
theString[2] = '\0';

If you wish theString to say "abode", then you must copy those characters into the array.

strcpy(theString, "abcde");

The standard library string functions are included in the program with:

#include <string.h>

The strcat() function is used to concatenate strings.

strcpy(theString, "abc");
strcat(theString, "123");

So in your case:

char elCorteIngles[20] = "Av. Jaime III";
char *paginasAmarillas;

paginasAmarillas = elCorteIngles;

strcpy(paginasAmarillas, "ABCDE");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.