简体   繁体   中英

How do you input a string inside each block of memory in an array?

I want to access strings from an array, just like one can access integers from arrays, say A={1,2,3} then upon calling A[0] one would get 1 . Similarly what should be done for strings such that A={a,b,c} so upon calling A[0] I get a .

I tried this for the input,

char in[1000];
for (i=0;i<5;i++)
{
    in[i]="A";
    printf("in is %f",in[i]);
}

but I am getting a warning assignment makes integer from pointer without a cast

You should use %s for strings not %f .

  • %s - Strings (character arrays in case of C)
  • %c - Characters
  • %f - floats
  • %d - int
  • %ls - long int

Your example :

int i=0;
char in[1000];
for (i=0;i<5;i++)
{
    in[i]='A';
    printf("in is %c\n",in[i]);
}

Sample code : example

Since you are making a character array, you need to supply a character value to it.

Change your code to

in[i]='A'
printf("in is %c",in[i]);

Hope this helps :)

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