Thread: relationship between struct array & malloc

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    relationship between struct array & malloc

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define STR_LENGTH 30
    
    void memoryAlloc (int a);
    void display(struct book_info* a, int count);
    
    struct book_info {
    	char author[STR_LENGTH];
    	char title[STR_LENGTH];
    	char pg[STR_LENGTH];
    };
    
    int main(int argc, char* argv[])
    {
    	int count;
    	int i;
    	fputs("Pease enter how many book to record: ",stdout);
    	scanf("%d",&count);
    	memoryAlloc(count);
    	fflush(stdin);
    
    	struct book_info* bookArray[count]; //variable cannot put in
    	
    	for (i = 0; i < count; ++i){
    
    		printf("Book %d\n",i+1);
    
    		fputs("Name of Author: ",stdout);
    		fgets(bookArray[i].author,STR_LENGTH,stdin);
    		fflush(stdin);
    
    		fputs("Title of the Book:",stdout);
    		fgets(bookArray[i].title,STR_LENGTH,stdin);
    		fflush(stdin);
    
    		fputs("Number of Page: ",stdout);
    		fgets(bookArray[i].pg,STR_LENGTH,stdin);
    		fflush(stdin);
    	}
    
    	display(bookArray, count);
    	free(bookArray);
    	return 0;
    }
    
    void display(struct book_info* a, int count){
    	int i;
    	for (i = 0; i < count; ++i){
    		printf("Book %d\n",i+1);
    		fputs("-----------------------------",stdout);
    		printf("Name of Author: %s",a[i].author);
    		printf("Title of the Book: %s",a[i].title);
    		printf("Number of Page: %s",a[i].pg);
    		fputs("-----------------------------",stdout);
    	}
    }
    
    //dynamic memory allocation function 
    void memoryAlloc (int a){
    	int* arr;
    	arr = (int*)malloc(a * sizeof(int));
    	if (a == NULL){
    		fputs("Filed to allocate memory!!!",stdout);
    		exit(1);
    	}
    }
    Hey guys, It might be a dumb question, but I really need your help.
    I'm trying to get a number from a user and then create an array with the number of index the user put in.
    For example, if a user input 3, array[3] will be created.
    I know that "int array[count]" won't work because a variable cannot be put in.
    So I made a function that allocates dynamic memory.
    However, I can't link it with struct.
    Anyone can give me a solution?

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Once you declare the dynamically allocated memory in memoryAlloc() how do you access it in your main() function?

    In your display function you pass in a pointer to your structure book_info, but then access the members of that structure as though you passed in an object to it.

    Why do you use fputs() with the stdout as a parameter? wouldn't puts() or printf() be more appropriate? Not saying you can't do it your way, just a little unorthodox

    I can't link it with struct
    I don't quite understand what you mean by this phrase. What do you want to do? Create an array of structure pointers and then access each one via an index? Shouldn't you be doing something like this if that is what you want to do:
    Code:
    struct_array = (book_info *)malloc( count*sizeof(book_info) );
    /* Or this way */
    struct_array = (struct book_info *)malloc( count*sizeof(struct book_info) );
    
    /* Now access the different books like this */
    printf("%s\n", struct_array[0]->author);
    Minor note: When you use fgets() to collect input with stdin as a parameter, the new-line character when you press enter is also included in your string. I don't know if you want it there or not, but just making you aware it will automatically be included in all the strings you enter.
    Last edited by Swarvy; 11-07-2010 at 10:45 PM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Never fflush(stdin)!

    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    2) What do you mean by "linking" it with the struct??

    If you are trying to allocate memory for an array of structs then just malloc n structs.

    Code:
    struct myStruct{
     int s;
    };
    
    int n;
    scanf("%d",&n);
    struct myStruct* array = malloc(n * sizeof(struct myStruct));
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Oh, Thanks for super fast reply
    What you wrote is exactly what I need to know.
    I really don't know the differences and effectiveness between puts() and printf() (i know the differences between puts() and fputs() though)
    Besides, I had to flush the buffer every time after using fgets(). Is there better and safer way to receive data from a user?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Another good advice. Thanks.
    So, fflush(stdin) gives an unexpected result when the buffer is already empty.
    I shouldn't use it unless there is surely unwanted data in the buffer.
    However, If I don't flush the buffer in this case, it won't allow me to input (based on using fgets())

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is there better and safer way to receive data from a user?
    Typically, you would use fgets() to read ALL user input into a temporary buffer.

    Then you would use sscanf() or strtol() (or several other strtox functions) to parse that buffer to extract the data you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM