Thread: string doesn't print in structure

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    string doesn't print in structure

    I am trying to create the record of student without string.h header file

    My code

    Code:
    #include<stdio.h>
    
    struct student
    {
       char name [10];
       int age;
       float hight ;
       char section;
       
    }student1;
     
    int main()
    {
    	student1.name[10] = "shyama";
    	student1.age = 18;
    	student1.hight = 5.8;
    	student1.section = 'A';
         
        printf(" name %s \n", student1.name);	
        printf(" age %d \n",  student1.age);  
        printf(" hight %f \n", student1.hight);
        printf(" section %c \n", student1.section);   
     
       return 0;
    }
    Result
    name
    age 18
    hight 5.800000
    section A

    Why the name of student doesn't print ?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Probably because student1.name[10] is not an array (or char *) but... a single character.

    You'll need to copy the student's name into the array somehow

    Maybe
    Code:
    snprintf(student1.name, 10, "%s", "shyama");

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > student1.name[10] = "shyama";

    Your compiler should warn you about this.... Up your warnings.

    name[10] sets you up indices from 0 through 9. Maybe use strcpy(student1.name , "shyama")

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print string with structure
    By vead in forum C Programming
    Replies: 3
    Last Post: 01-22-2018, 11:02 PM
  2. printf doesn't print a string
    By lmanukyan in forum C Programming
    Replies: 5
    Last Post: 12-27-2015, 05:58 PM
  3. doesn't print what it's supposed to
    By fane in forum C Programming
    Replies: 5
    Last Post: 02-01-2014, 07:31 PM
  4. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM

Tags for this Thread