Strings in C

September 24, 2010 at 06:53:03
Specs: Windows 7
Hello, I'm new to C programming and C. I would appreciate it if some one could tell me what i'm doing with this string.

//Declaration of 2D Array; 5 names and 40 chars in length
char stringarray[5][40];

//Function call; loop 5 times; in main
for(i=0; i<5; i++)
{
stringarray[i] = get_name();
}

//returns string(name) to main function
char get_name()
{
char temp[512];
printf("Enter name: ");
scanf("%s", temp);

return temp[512];
}


See More: Strings in C

Report •

#1
September 24, 2010 at 06:53:51
Also, this is where i'm getting error at:

stringarray[i] = get_name();
ERROR: incompatible types in assignment


Report •

#2
September 24, 2010 at 12:15:56
You could pass the string as an argument to the get_name function:

include <stdio.h>
int get_name(char temp[]);

int main()
{
char stringarray[5][40];
int i;

//Function call; loop 5 times; in main
for(i=0; i<5; i++)
   get_name(stringarray[i]);

for(i=0; i<5; i++)
   printf("%s\n", stringarray[i]);

}


int get_name(char temp[])
{
   printf("Enter name: ");
   scanf("%s", temp);
}


Report •
Related Solutions


Ask Question