Connect with us

Print the string of structure

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Feb 9, 2018.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    I want to print the string of structure what's the problem in my code. How to print the string of structure

    Code:
    #include<stdio.h>
    struct profile
    {
       char name[10];
       int number;
    };
     
    int main()
    {
        struct profile account; 
        account.number = 132; 
        account.name[10] = "Joy";
     
     unsigned int i;
     
     for(i =0; i < 10; i++);
     
       printf(" name %s \n",account.name[i]);
        
       printf(" number %d \n",account.number);
     
       return 0;
    }
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,214
    2,695
    Jan 21, 2010
    Do you understand the c for statement?

    Do you understand how a string is stored?

    Do you know how you determine the length of a string?

    Do you understand loops at all?

    You've either said yes to these questions (or ignored them) previously.

    Your question indicates the answer is probably "no" to all of them.

    So where should we start?
     
  3. vead

    vead

    473
    14
    Nov 27, 2011
    Yes I understand statement and loop in c programming

    string
    Code:
    #include <stdio.h>
     
    int main()
    {
        char name[4] = "Joy";
     
        printf("Name is %s\n",name);
     
        return 0;
    }
    Name is Joy

    length of a string
    Code:
    #include <stdio.h>
    int main()
    {
        char name[100], i;
    
        printf("Enter a string: ");
        scanf("%s", name);
    
        for(i = 0; name[i] != '\0'; ++i);
    
        printf("Length of string: %d", i);
        return 0;
    }
    Enter a string: Joy
    Length of string: 3
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    Why the for loop at all?
    Compare these two of your code sections:
    Code:
       char name[10];
    ...
        printf(" name %s \n",account.name[i]);
    Code:
    char name[4] = "Joy";
    ...
        printf("Name is %s\n",name);
    What will the code from your first post print, what will the code from your post #3 print? Why is there a difference and where?

    By the way: in your post #1 you wrote:
    Without telling us what you expect and what you get instead it is hard to tell where the problem is since you didn't tell us what the problem is. Try to be more precise in your questions.

    You may want to study a bit more strings and string handling in C, e.g. from this source.
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    I think I am missing some basics.

    I am explaining why the loop

    loop is use to check the size of array and store to array element

    Code:
    #include<stdio.h>
     
    int main (void)
    {
        int number[5]= {1,2,3,4,5};
    
        printf("array element : %d \n", number[0]);
        printf("array element : %d \n", number[1]);
        printf("array element : %d \n", number[2]);
        printf("array element : %d \n", number[3]);
        printf("array element : %d \n", number[4]);
       
        return 0;
    }
    Second program

    Code:
    #include<stdio.h>
     
    int main (void)
    {
        int i;
        int number[5] = {1,2,3,4,5};
       
        for ( i = 0; i < 5; i++)
        {
            printf("array element : %d \n", number[i]);
        }
           
        return 0;
    }
    
    
    Third program
    Code:
    #include<stdio.h>
    int main (void)
    {
        int i;
        int number[4];
        int size;
       
        printf("array size :" );
        scanf("%d",&size);
       
        for(i=0; i<size; i++)
        {
            printf("print array element ", number[i]);
            scanf("%d",&number[i]);
        }
       
        return 0;
    
    }
    These all program show the same output

    print array element : 1
    print array element : 2
    print array element : 3
    print array element : 4
    print array element : 5

    so when we have to store limited number then loop will use

    I think when I don't know the size of string, then I have to use loop
     
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    There is no intrinsic datatype "string" in C. A string is only an array of characters with the addition that usually the end of the string is indicated by a null character ("\0").
    Why do you think there is a correlation between knowing the size of the array and using a loop - or not?

    Please read the information in the link I gave and answer the questions in post #4.
     
  7. vead

    vead

    473
    14
    Nov 27, 2011
    This section will print the content of string.
    Code:
    char name[4] = "Joy";
    ...
        printf("Name is %s\n",name);
    This section will not print the string
    the name of string is wrong, it should name
    Code:
     char name[10];
    ...
        printf(" name %s \n",account.name[i]);

    There is difference between string name
     
  8. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    It is not.
    What's the diffference between
    Code:
    - account.name
    and
    - account.name[i]?

    It seems you still haven'tgot to know the difference between an array definition (e.g. int name[20]) and an array element (e.g. name).
    Learn about arrays, then you'll understand strings as a special kind of arry.
     
    Last edited: Feb 9, 2018
  9. vead

    vead

    473
    14
    Nov 27, 2011
    Please look at post #5

    I understand array in c. I have written program for array in post #5

    I know the difference between this two

    int number[5] = {1,2,3,4,5};
    Array storing 5 number's

    int number[5] = {"12345"};
    string of 5 numbers with a null terminator.
     
    Last edited: Feb 9, 2018
  10. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    What's the difference between
    Code:
    - account.name
    and
    - account.name[i]?
    Please answer this direct question. I'm sorry, the brackets didn't show up in the original post. I had to re-format the text for the brackets to be visible in a code box.
     
  11. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,214
    2,695
    Jan 21, 2010
    Vead,

    Imagine you asked me to prove I could drive a car.

    Imagine I replied with two photos showing the car in different places.

    Have I demonstrated I can drive a car?

    Your attempts to show your understanding of C is very similar. I suspect that you don't understand much at all, and your inability to answer questions tends to confirm that.

    You still seem to believe that there is a string type in C.

    I am not confident that you fully understand for loops. I asked a question about your understanding of the for loop and how it relates to another type of loop (which you ignored).
     
  12. vead

    vead

    473
    14
    Nov 27, 2011
    printf("Print content of account.name : %d \n", account.name);

    account.name = 5

    This statement will print the content of account.name. so if the account.name = 5 , then this will print 5

    printf(" Print content of account.name: %d \n", account.name )

    This statement will print the content store in array account.name, where i is array index

    if i = 4

    int account.name[4]= {1,2,3,4,5};

    account.umber[0] = 1
    account.umber[1] = 2
    account.umber[2] = 3
    account.umber[3] = 4
    account.umber[4] = 5
     
    Last edited: Feb 9, 2018
  13. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    But: your code defines account.name as follows:
    Code:
    account.name[10] = "Joy";
    Therefore, what will
    Code:
    printf("Print content of account.name : %d \n", account.name);
    print?
    And what will
    Code:
    printf("Print content of account.name : %d \n", account.name[i]);
    print for e.g. i=0?
     
  14. vead

    vead

    473
    14
    Nov 27, 2011
    This will not print anything because of %d this is use for numbers and you have initialized string
    This will store only one number
    account.name[o]= ?;
     
  15. vead

    vead

    473
    14
    Nov 27, 2011
    I have explained with example please look at post #5

    see the difference between both code
    Code:
    #include<stdio.h>
     
    int main (void)
    {
        int number[100]= {1,2,3,4,5};
    
        printf("array element : %d \n", number[0]);
        printf("array element : %d \n", number[1]);
        printf("array element : %d \n", number[2]);
        printf("array element : %d \n", number[3]);
    
      .
        printf("array element : %d \n", number[100]);
    
      
        return 0;
    }
    Code:
    #include<stdio.h>
     
    int main (void)
    {
        int i;
        int number[100] = {1,2,3,4,5.... };
      
        for ( i = 0; i < 101; i++)
        {
            printf("array element : %d \n", number[i]);
        }
          
        return 0;
    }
    
     
  16. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,214
    2,695
    Jan 21, 2010
  17. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    O.K., my bad, sorry.
    With reference to the original question in post #1 what I really meant to ask is:

    Your code defines account.name as follows:
    Code:
    account.name[10] = "Joy";
    Therefore, what will
    Code:
    printf("Print content of account.name : %s \n", account.name);
    print?
    And what will
    Code:
    printf("Print content of account.name : %s \n", account.name[i]);
    print for e.g. i=0?
    And why would you refer to
    Code:
    account.name[i]
    in a loop instead of referring to account.name?
     
  18. vead

    vead

    473
    14
    Nov 27, 2011
    I am really sorry

    Maybe i do not understand what you want to ask. Can you give me another chance. I will do my best
     
  19. vead

    vead

    473
    14
    Nov 27, 2011
    I think this will print the whole string. "Joy"

    I made program for this

    Code:
    #include <stdio.h>
    int main()
    {
        char account.name[10] = "Joy";
       
        printf("Print content of account.name : %s \n", account.name);
       
        return 0;
    }
    
    This program doesn't print anything but when I remove dot(.) then it print whole string

    same for this if I remove . then this will print the whatever value store in i = 0
     
  20. Harald Kapp

    Harald Kapp Moderator Moderator

    9,405
    1,921
    Nov 17, 2011
    No wonder, my quote was from your original program where name[] is used within a structure, hence it neeeds to be referred to as account.name. It doesn't help to use code snippets out of context. I therefore specifically referred to your post #1 to put my question in the right context.

    What do you mean by this? This makes no sense as "i=0" is no variable and therefore nothing can be stored there.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-