Thread: How to print string with structure

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    How to print string with structure

    How to print string with structure

    Code:
    #include<stdio.h>
    struct account
    {
       char name;
       int number;
    };
     
    int main()
    {
        struct account profile;
     
     profile.name = 'j';
     
     profile.number = 99;
       printf(" name %c \n",profile.name); 
       
       printf(" name %d \n",profile.number); 
     
       return 0;
    }
    name j
    name 99

    I want to print string "jack"
    Code:
    #include<stdio.h>
    struct account
    {
       char name[5];
       int number;
    };
     
    int main()
    {
        struct account profile;
     
     profile.number = 99;
     
     profile.name[5] = "jack";
     
     unsigned int i;
     
     for(i =0; i < 5; i++);
       printf(" name %s \n",profile.name[i]); 
       
       printf(" name %d \n",profile.number); 
     
       return 0;
    }
    assignment makes integer from pointer without a cast [-Wint-conversion]
    profile.name[5] = "jack";
    Last edited by vead; 01-22-2018 at 09:05 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that if you want to copy a string, you need to copy over its content, e.g., by using strcpy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    Recall that if you want to copy a string, you need to copy over its content, e.g., by using strcpy.
    what's the wrong in program

    Code:
    #include<stdio.h>
    struct account
    {
       char name[5];
       int number;
    };
     
    int main()
    {
        struct account profile;
     
     profile.number = 99;
     
     profile.name[5] = "jack";
    
       printf(" name %s \n",profile.name); 
       
       printf(" name %d \n",profile.number); 
     
       return 0;
    }
    warning: assignment makes integer from pointer without a cast [-Wint-conversion]
    profile.name[5] = "jack";
    ^
    Where is mistake in program. I couldn't figure out mistake

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vead
    Where is mistake in program. I couldn't figure out mistake
    • profile.name is an array of 5 char. Therefore, the valid indices of profile.name are 0, 1, 2, 3, and 4. Hence profile.name[5] accesses the array of out bounds, and is therefore a mistake.
    • "jack" is a string literal, i.e., an array of 5 char. Therefore, when you try to assign it to something, it is converted to a pointer to its first element. Even though profile.name[5] is wrong because it accesses the array out of bounds, the type of profile.name[5] is char (i.e., an integer), hence the compiler complains that you attempted to assign a pointer to a char ("assignment makes integer from pointer without a cast").
    • profile.name is an array of 5 char. Therefore, you cannot assign to it because assigning to an array is illegal. Hence, you cannot fix profile.name[5] = "jack"; by changing it to profile.name = "jack";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. print string
    By Miss Punjaban in forum C Programming
    Replies: 2
    Last Post: 04-15-2013, 01:19 AM
  2. assign string to the string variable inside structure!
    By king_zart in forum C++ Programming
    Replies: 20
    Last Post: 12-09-2012, 11:37 AM
  3. string print from 2 files
    By l_l in forum C Programming
    Replies: 4
    Last Post: 07-25-2012, 02:51 PM
  4. Structure to function (to print)
    By hello234 in forum C Programming
    Replies: 11
    Last Post: 11-02-2008, 02:58 PM
  5. Replies: 12
    Last Post: 10-14-2003, 10:17 AM

Tags for this Thread