Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-13-2012, 03:09 AM   #1
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
structure inside a structure in C

Pls help me with this programm..

#define BUFFSIZE 256
#define SMALL_SIZE 16
#define MED_SIZE 64
#define MAX  16
#define PORT 5555

char buffer[BUFFSIZE];

struct students
    int n;
    char Name[MED_SIZE];
    char Status[SMALL_SIZE];

struct Sending{
    int seqno;
    struct students class;
    struct Sending *next;

int main(){
     struct Sending *snd=NULL;
//  struct students *td =NULL;
//  char buf[BUFFSIZE] = "\0";
    snd = (struct Sending *)malloc(sizeof(struct Sending));

    memset(snd.class,0,sizeof(struct students));
    strcpy(snd.class->Name ,"PRIYA");
    printf("%s\n" ,snd.class->Name );
Old 09-13-2012, 03:46 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 6,753

Rep: Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809Reputation: 1809
memset(snd.class,0,sizeof(struct students));
this line will segfault because snd.class is not initialized. malloc some memory before usage
Old 09-13-2012, 04:06 AM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,648

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
'snd' is a pointer, so it won't compile on the first place
Old 09-13-2012, 06:52 AM   #4
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334

    memset(snd->class,0,sizeof(struct students));
    strcpy(snd->class.Name ,"PRIYA");
    printf("%s\n" ,snd->class.Name );

Avoid strcpy(); use strncpy() instead to avoid the possibility of overrunning a buffer.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to Initialize an array of string literals inside a structure Jusctsch Programming 2 11-08-2011 05:22 PM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 10:18 AM
Home Jail Folder Structure like Gobolinux Directory Structure luispt Linux - General 3 07-26-2008 07:46 PM
counting number of lines inside a directory structure. vl@d Linux - General 4 11-20-2006 01:50 PM
structure os2 Programming 6 05-29-2005 08:39 PM

All times are GMT -5. The time now is 05:10 AM.

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