Herman Herman - 1 month ago 11
C Question

Assigning array of pointers to string

I have tried searching on the web but found nothing like what I am trying to do. Hope you can help me out.

Problem: I have 2 char arrays, namely

string
and
string2
. Both are initially empty. I then give 2 inputs into these strings. I want to assign the value in string using pointers to string2.

Here is my code:

int main(void) {
char string[2][MAXLEN];
char string2[2][MAXLEN];
char *pointer[2];
pointer[0] = &string2[0];
pointer[1] = &string2[1];
scanf("%s", string[0]); //Assume i scan in "ab"
scanf("%s", string[1]); //assume i scan in "cd"

//Now string[0] contains "ab" and string[1] contains "cd"
//I want to deferences the pointer and assign "cd" to it, so string2[0] will contain "cd"

*pointer[0] = string[0];
printf("%s", string2[0]); //but this does not print "cd"
}


*Edit
I understand i can use strcpy, but im trying to learn to do it using pointers. Any help in this regards would be great.

Answer

Because you are not copying whole structures of info that the compiler can understand, you need to copy each element of the array individually. Usually this is done with a for loop checking for NUL or size, but I am cheating and just showing you the syntax that would do the copy you want:

#define MAXLEN 10

int main(void) 
{
    char string[2][MAXLEN];
    char string2[2][MAXLEN];
    char *pointer[2];
    pointer[0] = &string2[0];
    pointer[1] = &string2[1];

    // Replace scanf for simplicity
    string[0][0] = 'a'; string[0][1] = 'b'; string[0][2] = '\0';
    string[1][0] = 'c'; string[1][1] = 'b'; string[1][2] = '\0';

    // For loop or strcpy/strncpy/etc. are better, but showing array method of copying
    pointer[0][0] = string[1][0];
    pointer[0][1] = string[1][1];
    pointer[0][2] = string[1][2];

    printf("%s", string2[0]);

    return 0;
}

For pointers, you could do this:

#define MAXLEN 10

int main(void) {
  char string[2][MAXLEN];
  char string2[2][MAXLEN];
  char *pointer[2];
  pointer[0] = &string2[0];
  pointer[1] = &string[1];  // changed this

  string[0][0] = 'a'; string[0][1] = 'b'; string[0][2] = '\0';
  string[1][0] = 'c'; string[1][1] = 'd'; string[1][2] = '\0';

  *pointer[0]++ = *pointer[1]++;
  *pointer[0]++ = *pointer[1]++;
  *pointer[0]++ = *pointer[1]++;

  printf("%s", string2[0]);

  return 0;
}

The pointer magic above turns into:

  char temp = *pointer[1];  // Read the char from dest.
  pointer[1]++;  // Increment the source pointer to the next char.
  *pointer[0] = temp;  // Store the read char.
  pointer[0]++;   // Increment the dest pointer to the next location.

and I do it 3 times - one for each char of input. Surrounding that with a while() check against sourcePtr == '\0' basically turns it into strcpy().

One more fun example where a dereference might do what you expect:

typedef struct foo
{
    char mystring[16];
} FOO;

FOO a,b;

// This does a copy
a = b;

// This also does a copy
FOO *p1 = &a, *p2=&b;
*p1 = *p2;

// As does this
*p1 = a;

// But this does not and will not compile:
a.mystring = b.mystring;

// Because arrays in 'C' are treated different than other types.
// The above says: Take the address of b.mystring and assign it (illegally because the array's location in memory cannot be changed like this) to a.mystring.