accessing an array of structures

This is a discussion on accessing an array of structures within the C Programming forums, part of the General Programming Boards category; Hi, i am having trouble figuring out how to access the other elements of an array of structures that is ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    accessing an array of structures

    Hi,

    i am having trouble figuring out how to access the other elements of an array of structures that is returned by a function called sg_get_network_iface_stats .

    As is noted below, the function returns a pointer to the first element in an array.. which i can access fine. How do i access the other elements though, as i dont seem to have a handle on the actual array, only a pointer to the first element within it...
    .. I think trying to learn C is making me more dumbererrr, as the solution is probably simple :-(



    sg_network_iface_stats *sg_get_network_iface_stats( entries);
    int * entries;

    Description
    The sg_get_network_iface_stats function takes a pointer to an int, entries, which is filled with the number of network interfaces the machine has. This is needed to know how many sg_network_iface_stats structures have been returned. A pointer is returned to the first sg_network_iface_stats.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    Try accessing it as you would an array. After all, returning an array means returning a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    I have;

    ...
    sg_network_iface_stats p4;
    p4 = *sg_get_network_iface_stats(entries);

    With this i can use p4 to retrieve all of the structures info, eg: 'p4.speed'. Do you mean trying p4[1].speed ? I get this error attempting that;

    start.c: In function 'main':
    start.c:32: error: subscripted value is neither array nor pointer

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    With this i can use p4 to retrieve all of the structures info, eg: 'p4.speed'. Do you mean trying p4[1].speed ?
    No, since p4 is a struct, not an array. Rather:
    Code:
    sg_network_iface_stats *p = sg_get_network_iface_stats(entries);
    Now you access p[0].speed, p[1].speed etc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    !! ahh nice, ! it works. thanks for the help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  2. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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