LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-13-2012, 02:09 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
structure inside a structure in C


Pls help me with this programm..
Code:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>


#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, 02:46 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,928

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
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, 03:06 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,812

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

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Code:
...

    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.
 
  


Reply

Tags
structure


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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration