Thread: Vector in C

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    17

    Vector in C

    Hello, guys!
    I'm a begginner with C. And I wonder if there is Vector in C? Or how could I implement it? Need help. pls.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "vector"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Vectors are commonly used instead of arrays, because they expand automatically when new data is added to them.

  4. #4
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by midnight View Post
    Hello, guys!
    I'm a begginner with C. And I wonder if there is Vector in C? Or how could I implement it? Need help. pls.
    I don't think so, you implement that by using singly/doubly linked lists

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by midnight
    Vectors are commonly used instead of arrays, because they expand automatically when new data is added to them.
    No such functionality is provided by the C standard library. However, you can use the memory management functions available from <stdlib.h> to obtain dynamic array functionality.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    yup, I'll try to implement a dynamic array. thx a lot

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Do you know how could I get the size of structure? I need to sort values in it... but i dont know how

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use the sizeof operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct Data {
    char *name[50];

    } ;

    struct Data *the_array = NULL;
    int num_elements = 0; // Keeps track of the number of elements used
    int num_allocated = 0; // This is essentially how large the array is
    int control;
    struct Data *tmp;
    struct Data *a;
    int i;
    //void * realloc ( void * ptr, size_t size );

    int AddToArray (struct Data item)
    {
    if(num_elements == num_allocated) // Are more refs required?
    {
    // Feel free to change the initial number of refs
    // and the rate at which refs are allocated.
    if (num_allocated == 0)
    num_allocated = 0;
    else
    num_allocated ++;
    // Make the reallocation transactional
    // by using a temporary variable first
    //void *_tmp = realloc(the_array, (num_allocated * sizeof(struct Data)));
    tmp = (struct Data*)malloc(num_allocated*(sizeof(struct Data)));
    // If the reallocation didn't go so well,
    // inform the user and bail out
    if (!tmp)
    {
    fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
    return(-1);
    }
    // Things are looking good so far
    the_array = (struct Data*)tmp;
    }

    the_array[num_elements] = item;
    num_elements++;

    return num_elements;
    }

    int main (void)
    {


    while ((getchar() != 4)){
    getwchar(a->name);


    }




    }

    (

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Or you can use C++ which provide a vector, if you don't necessarily need to use C.
    They are almost identical, with some small differences (excluding the OOP stuff of course)

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Quote Originally Posted by C_ntua View Post
    Or you can use C++ which provide a vector, if you don't necessarily need to use C.
    They are almost identical, with some small differences (excluding the OOP stuff of course)
    I'd like to, but the program must be written in C. I have already it in C++, it works perfectly, but I don't know how it to do in C. This is the problem

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please post your code in [code][/code] bbcode tags.

    Do you really want name to be an array of 50 pointers to char? I suspect that you want it to be either an array of 50 char, or a pointer to char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    17
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct Data	 {
    char *name[50];
    
    } ;
    
    struct Data    *the_array = NULL;
    int     num_elements = 0; // Keeps track of the number of elements used
    int     num_allocated = 0; // This is essentially how large the array is
    int control;
    struct Data *tmp;
    struct Data *a;
    int i;
    //void * realloc ( void * ptr, size_t size );
    
    int AddToArray (struct Data item)
    {
            if(num_elements == num_allocated) // Are more refs required?
            {
                    // Feel free to change the initial number of refs
                    // and the rate at which refs are allocated.
                    if (num_allocated == 0)
                            num_allocated = 0; 
                    else
                            num_allocated ++; 
                    // Make the reallocation transactional
                    // by using a temporary variable first
                    //void *_tmp = realloc(the_array, (num_allocated * sizeof(struct Data)));
    			 	tmp = (struct Data*)malloc(num_allocated*(sizeof(struct Data)));
                    // If the reallocation didn't go so well,
                    // inform the user and bail out
                    if (!tmp)
                    {
                            fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
                            return(-1);
                    }
                    // Things are looking good so far
                    the_array = (struct Data*)tmp;
            }
    		
            the_array[num_elements] = item;
            num_elements++;
    	
            return num_elements;       
    }
    
    int  main (void)
    {
    
    	
    	while ((getchar() != 4)){	
     		getwchar(a->name);
     	}
     	printf("--------------------------\n");
     	
     
    	
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Another thing: avoid global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed