Resizing arrays

hi everyone...

well i´ve got another little problem here...

i have a structure like this:

1
2
3
4
5
6
7
typedef struct parametros{
    char* var;
    char* paramMethod[5];
    char* tamanho;
    int classe;
    struct parametros *prox;
}parametros;


but the field paramMethod can have an undefined number of positions...
so, i have to change its size, if there are more things to put on it...
here is a part of my code:
1
2
3
4
5
6
7
8
9
10
int contParam = 0;
    temp->paramMethod[0] = NULL;
    temp->classe = indiceLexClass;
    while(v){
        c_Name_Decl* n = v->nameDeclaration;
        if(n)
            temp->paramMethod[contParam] = getType(n->type);
        v = v->next;
        contParam++;
    }


v is a linked list containing the chars to put on the paramMethod positions...
but i need to resize it if the number is greater than the value i put on the declaration...

is there any way to do this?

It would be both safer and better if you used vectors, they function similar to arrays with the addition of being resizable.
http://cplusplus.com/reference/stl/vector/
but there´s any way to do this with simple char arrays?
You can't resize arrays, if you are using char* as strings, you should try std::string
If you really want a resizable array without STL, you can:
- create the array dynamically
- create a new array with the new size
- copy the elements from the first to the second array
- delete the first array
- redirect the pointer to the first array to the second
uhn...
thx bazzy...
to copy the elements I can just use memcpy?
and copy the entire array at once?
You can use sprintf http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

here is an example:
1
2
3
4
5
6
7
8
9
char *array = new char[6];
sprintf(array,"Hello");

char *temp = new char[11];
sprintf(temp,"%s",array);
delete[] array;
array = temp;

strcat(array," World");
NOTICE: this is almost C, C++ strings are better
1
2
3
//C++ rocks
string array = "Hello";
array += " World";

Last edited on
It seems someone hasn't heard of strcpy().

By the way, it'd be nice to stop suggesting C++ alternatives when OP is obviously using C.
Last edited on
yeah...
the main problem is that i should use the minimum of C++
but thanks bazzy...
i´ll try your sugestion here
=)
well, i decided to follow the advice and use vectors...
so i changed my structure...

1
2
3
4
5
6
7
typedef struct parametros{
    char* var;
    vector<char*> paramMethod;
    char* tamanho;
    int classe;
    struct parametros *prox;
}parametros;


but, when I do the push_back i get a segmentation fault

I´m doing this:

1
2
char* aux = getType(n->type);
            temp->paramMethod.push_back(aux);


what am I doing wrong with this?
temp may not be pointing to somewhere valid.
The declaration of parametros::paramMethod is fine.
but I create it and initialize like this, just over the push back:

parametros* temp = (parametros*)malloc(sizeof(parametros));


edit:
well I solved this error by creating another auxiliar vector...
and doing the push_back on it and them swaping it with de paramMethod...
but i think this is a very stupid solution...
=T
Last edited on
That's the problem. malloc() doesn't call constructors, and std::vectors need to be constructed before being used.
well, so i´ll do like i told above...
xD

thx a lot ppl for helping and sorry for anoying too much
^^
By the way, it'd be nice to stop suggesting C++ alternatives when OP is obviously using C.

Well it obviously wasn't obvious to the OP since he decided to start using vectors. Why do you keep writing these annoying and condescending statements helios? This website is cplusplus.com. If the OP wants help with C then they should either use another website or specifically state that they are only allowed to write C code in their assignment.
Condescending?
condescending
1. Assuming a tone of superiority, or a patronizing attitude.
That sentence was hardly condescending. Can you provide any concrete examples?

While you're doing that, consider why none of the people who actually replied to this thread took offense of what I said, or if they did they kept quiet, and you apparently did.
Topic archived. No new replies allowed.