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();
}

**

Recommended Answers

All 4 Replies

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


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

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

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]);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.