0

2-D string implementation

i am new to c.anyone please help me why this code is not working

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct{
        char name[2][10];
        }college ;

int main()
{
    college student;
    student.name={{"james"},
                      {"carter"}};

    printf("%s %s ", student.name);
    getch();
}

**

2
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by general2012
0

thnx bro.but still that prints only the first name.second name is not .how can i print both the names?

0

you have to tell the program what names you want to print. Here is one way to do it

printf("%s ", student.name[0]);
printf("%s ", student.name[1]);

or this
printf("%s %s ", student.name[0], student.name[1]);

And here is another way

int i;
for(i = 0; i < 2; i++)
   printf("%s\n", student.name[i]);
0

ok.it is quite clear now.actually

 printf("%s %s ", student.name)

in this line "name" which is the array name refers to the the begining of the array.thats why it only prints the first name.now the concepts become clear to me.thnx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.