Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 2, 2016

copy or assign one string to another string in a 2 dimensional array of pointers

copy or assign one string to another string in a 2 dimensional array of pointers


First of all, this is a test program, i wanna test something specific that i wanted to see if it works. Lets say that i wanna assign x to arr[0][4] and wanna keep this change so that arr[0][4] is x in the main function too:

#include   #include   #include     void changE(char *arr[10][5]);    int main(void){      char *arr[10][5];      arr[0][0] = "Johny";      arr[0][1] = "Tony";      arr[0][2] = "Tiki";      arr[0][3] = "Kitty";      arr[0][4] = "Douglas";      arr[1][0] = "Piki";      arr[1][1] = "Kati";      arr[1][2] = "Sathi";      arr[1][3] = "Dony";      changE(arr);      int i = 0;      int j;      for(i;i<=1;i++){          for(j=0;j<=4;j++){              printf("%s\n", arr[i][j]);          }          printf("\n\n\n");      }      return 0;  }  void changE(char *arr[10][5]){      char x[50] = "Tinky";      arr[0][4] = x;  }  

The problem is that i cant assign x to arr[0][4], the program just shuts down. I also tried strcpy, this:

void changE(char *arr[10][5]){        char x[50] = "Tinky";      strcpy(arr[0][4], x);  }  

Its the same thing with strcpy the program just shuts down. I can only do this:

void changE(char *arr[10][5]){      arr[0][4] = "Tinky";  }  

Which doesnt help me at all, considering that x is a string that i dont know(or a string from scanf). So if x comes from a scanf how can i assign x to arr[0][4]? Any help would be appreciated! Thanks :)

Answer by Weather Vane for copy or assign one string to another string in a 2 dimensional array of pointers


In the first function example that fails, you are trying to copy an automatic string variable to a string literal, which is read-only. That's why the function fails. arr[0][4] is a pointer to a string literal.

void changE(char *arr[10][5]){      char x[50] = "Tinky";      strcpy(arr[0][4], x);  }  

But in the second example that succeeds, you assign a string literal pointer directly (as you did in the first initialisation).

void changE(char *arr[10][5]){      arr[0][4] = "Tinky";  }  

As an aside, note the difference between

char name[] = "Alex";  

and

char *name = "Alex";  

In the first case, name is initialised with a copy of the data given, and it is modifiable.

In the second case, all name knows is a pointer, and the text it points to (a "string literal") is not modifiable.

Answer by l3x for copy or assign one string to another string in a 2 dimensional array of pointers


This method

void changE(char *arr[10][5]){      char x[50] = "Tinky";      arr[0][4] = x;  }  

doesn't work because you are assigning a variable with automatic storage (aka "local") duration. Once the function returns, it doesn't exist anymore. This is undefined behaviour.

This method

void changE(char *arr[10][5]){        char x[50] = "Tinky";      strcpy(arr[0][4], x);  }  

doesn't work because the arr[0][4] is pointing to a string literal. You can't modify a string literal. Again, this is undefined behaviour.

This method

void changE(char *arr[10][5]){      arr[0][4] = "Tinky";  }  

is the only correct way if you only have pointers and not allocated any memory for the pointees of those pointers.

So if x comes from a scanf how can i assign x to arr[0][4]?

For such scenario, you need to allocate memory and assign it to arr[0][4].

void changE(char *arr[10][5]){      char x[40] = "Tinky";     arr[0][4] = strdup(x); //POSIX, equivalent std C would be malloc()+strcpy()  }  

This is quite messy though. The array has pointers to string literals. But just arr[0][4] is pointing to a malloc'ed memory. You need to track such pointers if you want to be able to modify it or later when you call free() on it.

I would suggest you use just arrays instead of arrays of pointers if you want to be able to modify them as it's hard to keep track various pointers that are malloc'ed and with rest of them pointing at string literals.

Answer by haccks for copy or assign one string to another string in a 2 dimensional array of pointers


All the elements of arr are pointers to char. You initialized them in main, no memory is allocated. They just point to string literals.

The first version of changeE

void changE(char *arr[10][5]){      char x[50] = "Tinky";      arr[0][4] = x;  }   

doesn't work because x is an automatic local variable and returning pointer to it leads to undefined behavior.

In function

void changE(char *arr[10][5]){        char x[50] = "Tinky";      strcpy(arr[0][4], x);  }  

strcpy wouldn't work because arr[0][4] is pointing to a string literal and it can't be modified. You can modify arr[0][4] by allocating space for it

void changE(char *arr[10][5]){        char x[50] = "Tinky";      arr[0][4] = malloc(strlen(x) + 1);      strcpy(arr[0][4], x);  }     

Answer by Martin Zabel for copy or assign one string to another string in a 2 dimensional array of pointers


At first, you should initialize your array arr, so that, it contains no invalid pointers:

char *arr[10][5] = {0};  

Your first implementation of changE does not work because char x[50] is a local variable (allocated on the stack) initialized to "Tinky". You cannot excess this variable (by derefencing the pointer) once the function returns.

The implementation using strcpy does not work, because the destination memory is the string literal "Douglas" (as assigned in main) which cannot be overriden.

The pointer assignment from the first implementation is just fine, but you have to allocate memory from the heap as in this scanf example:

void changE(char *arr[10][5]){      char *x = (char *)malloc(100 * sizeof *x);      if(!x) exit(1); // error handling      scanf("%99s", x);      arr[0][4] = x;  }  

Answer by Vlad from Moscow for copy or assign one string to another string in a 2 dimensional array of pointers


String literals have static storage duration. So these assignments

arr[0][0] = "Johny";  arr[0][1] = "Tony";  arr[0][2] = "Tiki";  arr[0][3] = "Kitty";  arr[0][4] = "Douglas";  arr[1][0] = "Piki";  arr[1][1] = "Kati";  arr[1][2] = "Sathi";  arr[1][3] = "Dony";  

or this assignment in function changE

arr[0][4] = "Tinky";  

are coorect.

However you want to assign an element of the array with address of some character array that you are going to enter. In this case the storage duration of the array should be at least the same as the storage duration of the two-dimensional array.

The only reasonable approach is to dynamically allocate memory for readable characters and store their addresses in elements of the two-dimensional array.

You could use POSIX function strdup to do this. For example

arr[0][0] = strdup( "Johny" );  arr[0][1] = strdup( "Tony" );  arr[0][2] = strdup( "Tiki" );  arr[0][3] = strdup( "Kitty" );  arr[0][4] = strdup( "Douglas" );  arr[1][0] = strdup( "Piki" );  arr[1][1] = strdup( "Kati" );  arr[1][2] = strdup( "Sathi" );  arr[1][3] = strdup( "Dony" );  

Or you could write such a function yourself.

In this case you have to use also standard C function free to free all allocated memory by strdup or similar function.

The same approach should be used in the function changE

For example

void changE(char *arr[10][5]){      char x[50] = "Tinky";      arr[0][4] = malloc( strlen( x ) + 1 );      strcpy(arr[0][4], x);  }  

Answer by Andrea Dusza for copy or assign one string to another string in a 2 dimensional array of pointers


A possible solution is to have character arrays instead of pointers to characters like below. The strings' maximum length is 50 in this example.

void changE(char arr[10][5][50]);    int main(void){        char arr[10][5][50];        //...        strcpy(arr[0][0], "Johny");      strcpy(arr[0][1], "Tony");        //...      changE(arr);      //...  }  void changE(char arr[10][5][50]){      //this works now      char x[50] = "Tinky";      strcpy(arr[0][4], x);        //scanf works as usual      scanf("%s", &arr[0][4]);  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.