Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
06-11-2007, 01:38 PM
|
#1
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Rep:
|
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;
}
Thanks for your help.
Regards,
Ben
|
|
|
06-11-2007, 02:08 PM
|
#2
|
Member
Registered: Aug 2005
Posts: 330
Rep:
|
O course it doesn't.
I am no OO programmer (in fact I'm hardly a programmer  ) but here is something that works.
PHP Code:
#include <stdio.h>
#include <cstdlib>
#include <string.h>
struct name
{
char *array;
}variable;
int assign(name *,const char *);
int main()
{
char bla[]="The rain in Spain\n";
assign(&variable,bla);
fputs(variable.array,stdout);
}
int assign(name *variable, const char *str)
{
if(variable)
{
variable->array=(char*)calloc(strlen(str),sizeof(char));
strncpy(variable->array,str,strlen(str));
}
return 0;
}
Cheers.
|
|
|
06-11-2007, 02:39 PM
|
#3
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
Thanks for your help. Unfortunately I still don't understand it well. Can you tell me why it does not work the way I do it?
Can you provide some c code pherhaps, if it won't take too much effort.
Thanks.
Regards,
Ben
|
|
|
06-11-2007, 03:09 PM
|
#4
|
Member
Registered: Aug 2005
Posts: 330
Rep:
|
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.
|
|
|
06-11-2007, 03:20 PM
|
#5
|
Member
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205
Rep:
|
Hello,
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.
Cheers!
|
|
|
06-11-2007, 03:35 PM
|
#6
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
Quote:
That is actually C code. I enclosed it in php tags because it has syntax highlighting .
|
Lol.
Hey thanks guys, it is clear to me now. I'm going to try to write some code myself tommorow.
Regards,
Ben
|
|
|
06-13-2007, 10:24 AM
|
#7
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
This should work.
Code:
#include <stdio.h>
#include <string.h>
struct name {
char array[8];
};
int main( int argc, char * argv[] ){
struct name variable = {
.array = "string"
};
printf( "%s\n", variable.array );
strcpy( variable.array, "char's" );
printf( "%s\n", variable.array );
exit(0);
}
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.
--- rod.
|
|
|
06-13-2007, 03:38 PM
|
#8
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
:)
That works indeed.
Thanks theNbomr.
|
|
|
07-06-2022, 07:15 AM
|
#9
|
LQ Newbie
Registered: Jul 2022
Posts: 1
Rep:
|
String intialization
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);
}
here it is...
do have a try dear
|
|
|
07-06-2022, 08:34 AM
|
#10
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,207
|
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.
|
|
1 members found this post helpful.
|
07-06-2022, 12:07 PM
|
#11
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,315
|
Welcome to LQ tlha_bshir!
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).
Last edited by astrogeek; 07-06-2022 at 12:08 PM.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 06:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|