I want to know how to store a string as a C array element

Asked 2 years ago, Updated 2 years ago, 43 views

I want to create a two-dimensional array and store strings in each element. My code prints well, but it alerts me when I compile it. How do I create an array without warning?

char (*a[2])[14];
a[0]="blah";
a[1]="hmm";

printf(a[1]); //Good printout

c string array

2022-09-22 15:16

1 Answers

const char *a[2]; // an array with a length of 2 with a constant char pointer as an element
a[0] = "blah"; //save address of static string - points to read-only area of memory
a[1] = "hmm";

a[0][1] = 'a'//Error
chara[2][14]; // Typical two-dimensional array settings
strcpy(a[0], "blah"); copy to //stcpy
strcpy(a[1], "hmm");

a[0][1] = 'a'//ok


2022-09-22 15:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.