Unable to print the first character in a string in c

Viewed 60

im having a strange issue when trying to access the first character in a string. Im attempting to write a program that reads info from a file and separates it into a struct. however when i attempt to access a specific character in a string, i get a strange error. below is the error and code snippets.

Error:

/usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘char’
332 | extern int printf (const char *__restrict __format, ...);

code snippets. (not the whole program, just the lines that seem to be having issues)

char lineArray[40][101];

strcpy(lineArray[lineNum], validLine);

printf(lineArray[lineNum][0]);

when attempting to print the first character in an entry in lineArray, i get the above error. I get the same error when using strcpy in order to try and copy the first character into a different string. Any help would be greatly appreciated. Sorry if this is a foolish quetion, im still pretty new to C

1 Answers

I'd recommend you to change

printf(lineArray[lineNum][0]);

to printf("%c", lineArray[lineNum][0]);

Because the definition of printf function is int printf(const char *format, ...).
The first parameter must be format string, and the second parameter must be variables to be substituted into format.

On my recommended code, "%c" is meant to tell printf function to print a char variable that must be passed as a next variable.

Related