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.
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.
What do you mean by "vector"?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by midnight
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
yup, I'll try to implement a dynamic array. thx a lot
Do you know how could I get the size of structure? I need to sort values in it... but i dont know how
You can use the sizeof operator.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
#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);
}
}
(
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)
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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"); }
Another thing: avoid global variables.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)