LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-11-2007, 12:38 PM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
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
 
Old 06-11-2007, 01:08 PM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
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.
 
Old 06-11-2007, 01:39 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
 
Old 06-11-2007, 02:09 PM   #4
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
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.
 
Old 06-11-2007, 02:20 PM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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!
 
Old 06-11-2007, 02:35 PM   #6
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
 
Old 06-13-2007, 09:24 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-13-2007, 02:38 PM   #8
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
:)

That works indeed.

Thanks theNbomr.
 
Old 07-06-2022, 06:15 AM   #9
tlha_bshir
LQ Newbie
 
Registered: Jul 2022
Posts: 1

Rep: Reputation: 0
Wink 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
 
Old 07-06-2022, 07:34 AM   #10
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
Code:
man strncpy
 
1 members found this post helpful.
Old 07-06-2022, 11:07 AM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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 11:08 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
data structure with variable capacity shivaligupta Programming 2 01-31-2005 11:54 AM
error in accessing structure variable in kernel modules dypgrp Programming 3 01-17-2005 07:42 AM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration