Confusion about pointers

This is a discussion on Confusion about pointers within the C Programming forums, part of the General Programming Boards category; here's what i'm trying to do. i have an array of integers. i don't apriori know the size of this ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    Confusion about pointers

    here's what i'm trying to do.

    i have an array of integers. i don't apriori know the size of this array. (i get it from my i/p file.) now i first calloc the array by using an integer pointer. then i go back to my i/p file. what i have there are the (integer) values of each of the elements of the array. my question is, how do i read these elements while i loop over the array size?

    further, each of the array elements form the size of other arrays used in the program. so i've to calloc those arrays using these elements. what is the correct syntax for these calloc statements?

    i'd be very thankful if someone can answer these questions.

    thanks for your time,
    murali.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    /* Malloc enough to hold 10 ints */
    int *myarray = malloc(sizeof(*myarray) * 10);
    
    /* Now loop through the array, assigning it a values. */
    for (i = 0; i < 10; i++)
    {
      myarray[i] = i;
    }
    Personally, I'd avoid calloc().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 01:54 PM
  2. Massive Confusion with Pointers
    By guitarist809 in forum C++ Programming
    Replies: 8
    Last Post: 07-22-2008, 04:01 PM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. array and pointers confusion
    By int80 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 03:55 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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