Thread: Array of Pointers

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Array of Pointers

    Can someone tell me how the heck to get input into an array of pointers, via my code.

    insert
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {    
    	int granularity, *Arrayptr,iLoop,iCount,iarray;
    	char *arraysize;
    	char szBuf[10];
    		printf("Input the array size: \n");          		     //Ask the user for input		
    		arraysize = fgets(szBuf,sizeof(szBuf), stdin);   	     //Get the input from the user
    		iarray = atoi(arraysize);				             //Convert Input from string to integer
    		Arrayptr = malloc(iarray * sizeof(int));  		         //Reserve some memory on the heap for the array
    	             if((Arrayptr = malloc(iarray * sizeof(int))) == NULL){
                        (void)printf("Malloc failed");
                        return 1;
                     }
            if(Arrayptr != NULL){        
            printf("Please input values into array -1 to quit: \n");
                     for(iLoop = 0 ;; iLoop ++){
                     scanf("%d",Arrayptr[iLoop]);
                     iCount += Arrayptr[iLoop];
                     }
            }                            
    return 0;
    }
    I'm trying to use scanf but it's not working, then I want to take the value entered and keep a running total, I'm just not sure why it's not working.

    Thanks

    Andrew

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Arrayptr = malloc(iarray * sizeof(int)); //Reserve some memory on the heap for the array
    > if((Arrayptr = malloc(iarray * sizeof(int))) == NULL)
    Only call it once, then check for NULL

    > if(Arrayptr != NULL)
    You already checked for NULL, checking again adds nothing.

    > for(iLoop = 0 ;; iLoop ++)
    When does this loop exit?

    > scanf("%d",Arrayptr[iLoop]);
    You still need the &, as in
    scanf("%d", &Arrayptr[iLoop]);
    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.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Okay so I put the exit condition to iLoop <= iarray or in other words when the array size has been reached then exit.

    When I enter say
    2
    2
    2
    2
    2
    2
    2
    2
    etc...... up to 10 times the value returned by iCount seems to be 2008858765, I'm sure why I'm getting this return value.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Initialize (or set) iCount to zero. Right now it starts with a "random" value.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM