LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-15-2006, 04:20 PM   #1
anoosh
LQ Newbie
 
Registered: Aug 2004
Location: IRAN
Posts: 7

Rep: Reputation: 0
malloc


hi there.
I have these two structures below:
struct name{
char *firstn,*lastn;
};
typedef struct name NAME;
struct student{
NAME stname;
int *mark;
}*st;
typedef struct student STUDENT;
_________________________________________________________
I have 3 pointers:firstn,lastn and st and I'm going to use them as dynamic arrays.
please tell me how to allocate dynamic memory for that in C.
 
Old 03-15-2006, 05:41 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You answered your own question: you can "malloc()" each of the strings.

Stylistically, I'd lose the "typedefs".

If you really insisted on using typedef's, however, I'd *definitely* include them in your struct definition:
Code:
  /* POOR */
  struct name
  {
    char *firstn, *lastn;
  };
  typedef struct name NAME;
  ..
  NAME myName;

  /* BETTER */
  typedef struct name
  {
    char *firstn, *lastn;
  }
    name;
  ..
  name myName;


  /* BETTER STILL */
  #define MAXNAME 20
  struct name
  {
    char firstn[MAXNAME];
    char lastn[MAXNAME];
  };
  ..
  struct name myName;
And, if you're using C++:
Code:
  // BEST
  class Name
  {
    public:
      Name (const char *fname, const char *lname);
      char *getFirstName ();
      char *getLastName ();
    private:
      char firstn[MAXNAME], lastn[MAXNAME];
  };
  ..
  Name *myName = new Name ("Miles", "Standish");
PS:
If you're using C++, you might consider just using the Standard C++ "string" class. This eliminates the whole issue of what to "malloc()", when...

Last edited by paulsm4; 03-15-2006 at 05:45 PM.
 
  


Reply


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
malloc eagle683 Programming 6 05-22-2005 03:40 PM
problem using malloc in C huble Programming 10 03-27-2005 01:45 AM
malloc in c programming saiz66 Programming 18 10-04-2004 03:00 PM
malloc() vijeesh_ep Programming 4 08-25-2004 04:50 PM
about malloc eshwar_ind Programming 11 02-18-2004 04:41 PM


All times are GMT -5. The time now is 07:39 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration