Like Tree1Likes

realloc help

This is a discussion on realloc help within the C Programming forums, part of the General Programming Boards category; I have this error 'realloc' : cannot convert parameter 1 from 'stock' to 'void *' but i cant seem to ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20

    realloc help

    I have this error 'realloc' : cannot convert parameter 1 from 'stock' to 'void *' but i cant seem to find why ive read about realloc() but i know i need to use malloc() first or calloc() i just cant seem to get the jist of the idea thanks

    Code:
    					printf("Lists stock\n");{
    		
    	FILE *fp;			//file pointer
    		fp=fopen("stock.txt","r");
    
    
    
    
    		stock mylist[50];
    
    
    	while(1) /* infinite loop */
    
    
    	
    	fp=fopen("stock.txt","r");
    
    
    malloc( 1000 * sizeof( struct stock) == NULL );
    
    
      Nfields = fscanf(fp, "'%d %s %d\n", &mylist[i].product_id, &mylist[i].product_name, &mylist[i].stock_level); /* fscanf returns the number of fields it successfully read */
    
    
      if(Nfields != 3)  /* if we didn't read three fields, we're at the end of the data */
    
    
    	  break;
    
    
       mylist = realloc(mylist[i], (i +1) * sizeof(struct stock)); /* or whatever you called the structure */ 
    
    
      if(!mylist)
    
    
    
    
    mylist[i].product_id;
    strcpy(mylist[i].product_name,pointer);
    mylist[i].stock_level;
    {
    printf("Product Name:%s\n",mylist[i].product_name);
    
    
    printf("Product_ID:%d\n ",mylist[i].product_id);
    	
    printf("Stock Level:%d\n\n",mylist[i].stock_level);  
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Your indentation is atrocious. If your code looks that crappy, it will be easy to make mistakes and hard to find or fix them. Read this: SourceForge.net: Indentation - cpwiki.

    It's hard to give complete help when you omit relevant pieces of code like the definitions of stock and struct stock types.

    I think you need to do some reading up on arrays and structs, and dynamic memory allocation. You don't actually need to use malloc/calloc first, the right call to realloc, or the right initialization of variables passed to realloc, will make it behave like malloc.
    Code:
    malloc( 1000 * sizeof( struct stock) == NULL );
    That's broken in several ways. You don't set the value of malloc to anything, so you just allocate memory and throw it away, causing a memory leak. That == NULL does not belong in there. You should check the return value of malloc (or the pointer you assign it to) for NULL.
    Code:
    stock mylist[50];
    ...
    mylist = realloc(mylist[i], (i +1) * sizeof(struct stock));
    You can't assign a new chunk of memory to an array address. You probably want to make mylist a struct stock pointer, without the array bit*. Also, what is i here? It is potentially uninitialized, meaning you could realloc any number of bytes. Also, decide if it is mylist or mylist[i] you want to malloc/realloc memory into. They are not the same thing. As for your specific error message, "cannot convert parameter 1 from 'stock' to 'void *'", realloc takes a pointer to memory already allocated by malloc/calloc/realloc, or NULL (if NULL, it behaves just like malloc). But mylist[i] is a stock object, not​ a pointer to anything.

    Also, you need to use a temporary value to store the result of realloc, and check it for NULL, before you assign it to your old pointer. Otherwise you run the risk of another memory leak.

    * I can't tell, without the definition of stock and struct stock types, whether that is actually an array of pointers, though the error message you get suggests it's not. If it is, please change it. typedef'ing pointers is generally not a good idea.
    Last edited by anduril462; 05-29-2012 at 02:33 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    is there an easier way to print out my entire text file?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    If you just want to print it, there is no need to read it into an array, whether statically or dynamically allocated. Just have a single struct, statically declared, read into that and print the data. Then your next iteration of the loop will read into the same struct, overwriting the old data, then you will print that out.
    Code:
    struct stock s
    fp = fopen
    while (fscanf(fp, "...", &s.product_name, ...) == 3)
        print stuff
    fclose(fp)

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    then it just prints out loads of random numbers
    Code:
    case 3:
    					printf("Lists stock\n");
    {
    		
    	FILE *fp;			//file pointer
        fp=fopen("stock.txt","r");
    	stock mylist[50];
    
    
    
    
    
    
    while (fscanf(fp, "%s  %d  %d", &mylist[i].product_name,&mylist[i].product_id,&mylist[i].stock_level) == 10)
        	
    			printf("Product Name:%s\n",mylist[i].product_name);
    
    
    			printf("Product_ID:%d\n ",mylist[i].product_id);
    	
    			printf("Stock Level:%d\n\n",mylist[i].stock_level);  // start to process your data / extract strings here...
    		
    fclose(fp);
    
    
    	
    
    
    }
    	break;
    Last edited by dan.goodridge; 05-29-2012 at 03:02 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    How many things do you expect fscanf to read when you give it "%s %d %d"?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > while (fscanf(fp, "%s %d %d", &mylist[i].product_id,&mylist[i].product_name,&mylist[i].stock_level) == 10)
    Where the ........ did you get 10 from?

    You had 3 in your first post FFS.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    ok edited it
    Code:
    case 3:
    					printf("Lists stock\n");
    {
    		
    	FILE *fp;			//file pointer
        fp=fopen("stock.txt","r");
    	stock mylist[50];
    
    
    
    
    
    
    while (fscanf(fp, "%s,%d,%d",&mylist[i].product_name,&mylist[i].product_id,&mylist[i].stock_level) == 3)
          
    			printf("Product Name:%s\n",mylist[i].product_name);
    
    
    			printf("Product_ID:%d\n ",mylist[i].product_id);
    
    
    			printf("Stock Level:%d\n\n",mylist[i].stock_level);  // start to process your data / extract strings here...
    	
    			
    		
    fclose(fp);
    
    
    	
    
    
    }
    	break;	
    
    
    ;
    Last edited by dan.goodridge; 05-29-2012 at 03:13 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    This isn't Python, so merely indenting your printf statements wont make them all subordinate to the while loop. You need some curly brackets.

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    when i put in curly brackets it jumps the printf statement and goes straight to break.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Post your new code.

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    heres the code
    Code:
    case 3:					printf("Lists stock\n");
    {
    		
    	FILE *fp;			//file pointer
        fp=fopen("stock.txt","r");
    	stock mylist[50];
    
    
    
    
    
    
    while (fscanf(fp, "%s,%d,%d",&mylist[i].product_name,&mylist[i].product_id,&mylist[i].stock_level) == 3)
    {
    	
    			printf("Product Name:%s\n",mylist[i].product_name); // print product name
    
    
    			printf("Product_ID:%d\n ",mylist[i].product_id); //print product id
    
    
    			printf("Stock Level:%d\n\n",mylist[i].stock_level);  // print stock level
    	break;	
    }
    		
    fclose(fp);

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Look at lines 23 and 24. What does the 'break' keyword do?

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    o i have the break in the wrong place it just jumps to case 4 with it being at that point

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Cardiff, United Kingdom
    Posts
    20
    it was to print results then end program

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc
    By Dr Saucie in forum C Programming
    Replies: 13
    Last Post: 03-19-2010, 11:14 PM
  2. help with realloc()
    By stevend in forum C Programming
    Replies: 14
    Last Post: 11-04-2006, 02:37 PM
  3. realloc
    By siavoshkc in forum C Programming
    Replies: 29
    Last Post: 02-02-2006, 11:36 PM
  4. realloc
    By gustavosserra in forum C Programming
    Replies: 3
    Last Post: 08-05-2004, 07:35 PM
  5. realloc() ????????
    By RoshanX in forum C Programming
    Replies: 3
    Last Post: 10-12-2003, 07:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21