Maker Pro
Maker Pro

Print the string of structure

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

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
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?
 
Do you understand the c for statement?
Do you understand loops at all?
Yes I understand statement and loop in c programming

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

Do you know how you determine the length of a string?
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
 

Harald Kapp

Moderator
Moderator
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:
I want to print the string of structure what's the problem in my code. How to print the string of structure
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.
 
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
 

Harald Kapp

Moderator
Moderator
I think when I don't know the size of string, then I have to use loop
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.
 
answer the questions in post #4.
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
 

Harald Kapp

Moderator
Moderator
the name of string is wrong
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:
It is not.
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.
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:

Harald Kapp

Moderator
Moderator
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.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
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).
 
Please answer this direct question. .

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:

Harald Kapp

Moderator
Moderator
This statement will print the content of account.name. so if the account.name = 5 , then this will print 5
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?
 
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?
This will not print anything because of %d this is use for numbers and you have initialized string
And what will
Code:
printf("Print content of account.name : %d \n", account.name[i]);
print for e.g. i=0?
This will store only one number
account.name[o]= ?;
 
I asked a question about your understanding of the for loop and how it relates to another type of loop (which you ignored).
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;
}
 

Harald Kapp

Moderator
Moderator
This will not print anything because of %d this is use for numbers and you have initialized string
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?
 
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?

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

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]
same for this if I remove . then this will print the whatever value store in i = 0
 

Harald Kapp

Moderator
Moderator
This program doesn't print anything but when I remove dot(.) then it print whole string
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.

this will print the whatever value store in i = 0
What do you mean by this? This makes no sense as "i=0" is no variable and therefore nothing can be stored there.
 
Top