简体   繁体   中英

How do I store a string in an multidimensional array in C?

I feel silly for asking, but I had trouble finding my answer. How do I re-assign "Rose" to "Douglas"? It seems like I have to use a loop.

#include <stdio.h>

int main() {

  char arr[3][12]= { "Rose", "India", "technologies" };
  printf("Array of String is = %s,%s,%s\n", arr[0], arr[1], arr[2]);
  arr[0][0] = {"Douglas"};
  printf("Array of String is = %s,%s,%s\n", arr[0], arr[1], arr[2]);

    return(0);
}

You can do this with strcpy() :

strcpy(arr[0], "Douglas");

When using strcpy() , you will have to ensure that there is enough space in the destination to hold the string you're putting there (plus the terminating NUL character). In this case there is, because you have allocated 12 bytes for each string and "Douglas" will take 8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM