Metie Metie - 12 days ago 5
C Question

How to refer to a character of a member of an array structure in c?

I tried but I got an integer where the character should have been. Thanks for your help. The problematic part is at the 13th line.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct y{
char name1[30];
char name2[30];
} y;

void x(y *array){
int i;
for (i=0;i<2;i++)
printf("%d",array[i].name1[0]); /*<- Why is this integer?*/
}


int main()
{
y array[2]={
{ "apple", "battle"},
{ "cup", "dog" },
};

x(array);

return 0;
}

Answer

This is happening because you are using the type-specifier %d, which is used to print an int in the printf function, in order to print a character, you need to use the type-specifier %c.

printf("%c",array[i].name1[0]);