C equivalent of vector?

This is a discussion on C equivalent of vector? within the C Programming forums, part of the General Programming Boards category; I need to do something in C, but I am familiar with C++ where I could use a vector to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    C equivalent of vector?

    I need to do something in C, but I am familiar with C++ where I could use a vector to store things of any type and access them by an index #. Is there a fairly simple way to get similar functionality in C or does it depend on the specific data type you want to use?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Without creating your own vector type, you'd need to use an array, which is also available in C++. You can declare an array of any type, so if you need a struct, you can make an array of that struct.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Nothing but a plain old array I'm afraid.
    However in C99 you can use a variable when declaring the size of the array I believe.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by dougwilliams View Post
    I need to do something in C, but I am familiar with C++ where I could use a vector to store things of any type and access them by an index #. Is there a fairly simple way to get similar functionality in C or does it depend on the specific data type you want to use?
    If you want a dynamic vector (one that grows as you add more elements to it), you'll need to implement it yourself. Otherwise, just use an array.

    Unfortunately, C has no templates so you have to write code to deal specifically with different kinds of objects, since they are all of different sizes and types. You can write a function that handles a lot of this for you, but it's not going to be nearly as pretty as the equivalent using C++ vectors.

    It's possible to boil it all down to a few very simple-looking macro calls.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    I was fortunate enough to find a custom method for doing this with my data type:

    GTK Widget array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 06:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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