473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

malloc struct

Hi I'm having difficulties trying to malloc a struct this is what it looks like and how I'm approaching the problem.

Expand|Select|Wrap|Line Numbers
  1. struct Records
  2. {
  3.   char * fname;
  4.   char * lname;
  5. };
  6.  
  7. Records**   g_Recs; //Array of pointers
  8.  
  9.  
  10. void fakefunc()
  11. {
  12.  g_Recs = (struct StudentRecords **) malloc (sizeof(struct StudentRecord*));
  13.  g_Recs = (struct Records *) malloc (sizeof(struct StudentRecord));
  14.  
  15.  g_Recs->fname= (char *) malloc(strlen(buffer));
  16.  
  17.  
  18. }
Thats a very basic example of my code. I get an error that ->fname must point to class/struct/union/generic type.

I'm assuming that I am not mallocing the struct properly and thats is why im getting this erro. If anyone could help me with this, I would greatly appreciate it.
Feb 3 '10 #1
3 3801
weaknessforcats
9,208 Expert Mod 8TB
struct Records
{
char * fname;
char * lname;
};

Expand|Select|Wrap|Line Numbers
  1. Records**   g_Recs; //Array of pointers 
This is not an array of pointers. This is one pointer that is a pointer to a Record.

Remember, the name of the array is the address of element 0. Therefore, if g_Recs is an array of Records, then g_recs is the address of element 0. That makes g_recs the address of a Records.

That is, g_recs is a Records*.

To allocate an array of records you need to know how many Records are in the array. Let's say there are 20. So you allocate memory for 20 Records:

Expand|Select|Wrap|Line Numbers
  1. Records* g_recs = malloc(sizeof(Records) * 20);
Feb 3 '10 #2
Thank you for the clarification, but I'm still unsure of the meaning of the double star **. As well I'm not sure how i would malloc one of the members of the struct. If you or someone could show me an example of mallocing a struct that is declared the way mine is (with the **) I would greatly appreciate it. Thanks for your time
Feb 3 '10 #3
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. struct Records {
  2.     char *fname;
  3.     char *lname;
  4. };
  5.  
  6. // Relationship between object, pointer, and pointer-to-pointer.
  7. struct Records theRecord;
  8. struct Records *pRecord = &theRecord;
  9. struct Records **ppRecord = &pRecord;
  10.  
  11. // How to access a record.
  12. char *pText;
  13. pText = theRecord.fname;
  14. pText = pRecord->fname;
  15. pText = (*ppRecord)->fname;   // I'm not sure about this one.
Suppose sizeof(void*) on this platform is 4. Then sizeof(theRecord) is 8, sizeof(pRecord) is 4, and sizeof(ppRecord) is 4.

I don't know if the parentheses are needed in the last pText example.

In general to avoid confusion, you should have a very good reason for using double-start pointers-to-pointer. As a rule, you should never use triple-star pointers-to-pointer-to-pointer or beyond. There are exceptions but they don't arise often.

What are you trying to do that led you to believe that a double-star pointer would be helpful?
Feb 4 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

59
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
58
by: lasek | last post by:
Hi all..i'm here another time..for a simple question: #include <stdlib.h> #include <stdio.h> int main(void) { /* ** I know that someone else use different malloc ** instruction
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.