How to assign a string to a structure variable in C?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to assign a string to a structure variable in C?
Hi,
I want to assign a string to a structure variable in C, but I'm not sure how to do that. Can someone show me how it can be done please?
The way I am trying to achieve it is:
Code:
#include <stdio.h>
main()
{
struct name{
char array[8];
} variable;
//That goes well, but I'm not sure what to do next. I have tried a few things including this:
variable.array[8] = "string";
//But nothing works
return 0;
}
That is actually C code. I enclosed it in php tags because it has syntax highlighting .
You can't assign anything to an array, because it is a constant. That is right, some say that arrays and pointers are the same, only that arrays are constant pointers. I don't entirely agree with that, but, to some extent it is true. And more, one might have expected you to write variable.array = "string"; , it's a mistake I've also made.
In the sloppy code I posted, I create a struct and a pointer. Then, via calloc, I assign some memory and copy (strncpy) the char array to the array in the structure.
My guess is, after your questions, that in fact you are looking for C++ code. Check out <string> . I believe you'll like it.
In straight vanilla "C" you would "strncpy(variable.array, "string", sizeof(variable.array));", or "strcpy(variable.array, "string");"
You are not allowed to perform a byte assignment using an array such as you were trying in your samples. You must copy the string into the destination byte array one byte at a time. That is what the strcpy, strncpy, memcpy, etc functions perform for you.
I would suggest that you take a look at the man pages for the strcpy, strncpy, memcpy, etc functions to get a better understanding into what they offer, and how to use them.
The structure declaration is moved outside of any function/block. The structure definition can/should be inside any block. Initialization of the structure, per C99, is demonstrated. Assignment to the character array is demonstrated.
Hello Dear, You are trying to assign value directly to a string which is no valid way in c language programming you should also go through of my idea.
First try to include library of #include<string.h> at the top of your program.
This library contains many usefull functions like strcpy(copy string 2 to string 1),strcmp(two strings compare).
Lets see how is this done.
#include<string.h>
int main()
{
char fullname[20];
printf("--------------\n");
strcpy(fullname, "Talha Bashir");
printf("Your name : %s", fullname);
}
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
when assigning to a fixed length array like this it would probably be better to use strncpy as you can set the max number of bytes copied, strcpy could easily cause an overflow.
With your first post you have resurrected a thread which has been quiet for 15 years. If you have a question of your own or knowledge you would like to share it is preferred that you open your own thread in most cases.
Please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.
Also, please wrap your code and data snippets inside [CODE]...[/CODE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.