Davis8988 Davis8988 -4 years ago 66
C Question

How to pass char array and access it's value in C

I am a begginer at C and I want to understand how to pass a char array to a method. And in the method access the array's values.

I tried making an example but i keep getting errors :/

here's my example:

int checkCommandSyntax(char * text);

int main(int vcount, char* varr[])
{
char * text = "fg something";

checkCommandSyntax(text);


getchar();
}


int checkCommandSyntax(char * text)
{
int i = 0;

while (!(text[i] == '\0'))
{
printf("%s", text[i]);
i++;
}

}

Answer Source

Your code works fine besides one simple error. You are passing printf the argument %s which means it should print a string. You are trying to print one character, so change

printf("%s", text[i]);

to

printf("%c", text[i]);

%c denotes that you want to print a character value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download