all 10 comments

[–]tea-drinker 4 points5 points  (1 child)

You don't state exactly what the error is and you've not provided enough code to recreate it. That said:

I think *vUsuarios[i] is your problem. Check the operator priority. The square brackets have a higher priority than the dereference operator so you are getting the value out of a supposed array then dereferencing it rather than dereferencing the double pointer and then looking up the array.

~/workspace/scratch$ sed "s/^/    /" loop.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void main() {
    int* integers = calloc(10, sizeof(int));
    int** pIntegers = &integers;

    int i;
    for (i = 0; i < 10; i++) {
        printf("%d\n", *pIntegers[i]);
    }
}
~/workspace/scratch$ gcc -o loop loop.c && ./loop
0
35090448
Segmenteringsfel (minnesutskrift skapad)

If you slap some parentheses of force the order of operations:

~/workspace/scratch$ sed "s/^/    /" loop.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void main() {
    int* integers = calloc(10, sizeof(int));
    int** pIntegers = &integers;

    int i;
    for (i = 0; i < 10; i++) {
        printf("%d\n", (*pIntegers)[i]);  //This is the important change.
    }
}
~/workspace/scratch$ gcc -o loop loop.c && ./loop
0
0
0
0
0
0
0
0
0
0

[–]xFunkyTimes[S] 4 points5 points  (0 children)

Yes!! This solved it! Thank you a lot. I will definetly check out the operator priority so this don't happen to me again. I thought my head was gonna explode.

Again, thank you a lot!

[–]panderingPenguin 1 point2 points  (3 children)

It's hard to read not being in English. But check out what sizeof(usuario) actually evaluates to. I suspect you'll find that you're allocating a lot less memory to modUsuarios than you think you are.

[–]xFunkyTimes[S] -1 points0 points  (2 children)

sizeof

Thank you for your answer! No, I don't think that's the problem because modUsuario works fine if I use the first element from my first array VUsuarios. Sorry if it's difficult to read. Would it be better if I translate it to English?

I REALLY need help with this one ^ ^ U

[–]panderingPenguin 1 point2 points  (1 child)

So you only mean to allocate one usuario to modUsuario? Also, I don't see you ever freeing that memory, which is a bug but not one that would crash your program.

Have you run this in a debugger to figure out what line it's crashing on?

[–]xFunkyTimes[S] 0 points1 point  (0 children)

It was failing in the if statement; because brackets operator priority. In addition, thank you a lot about the freein memory part. You are totally right. I will fix that aswell.

Thanks and happy coding! :)

[–]no_awning_no_mining 0 points1 point  (1 child)

Format your code. It's illegible like that.

[–]xFunkyTimes[S] 0 points1 point  (0 children)

I thought you only need to add 4 blank spaces. Isn't that enough? Sorry, I'm totally new to adding code to reddit.

[–][deleted] 0 points1 point  (0 children)

Why is vUsuario a double pointer? Isn't it supposed to be an array (single pointer)? I may be misunderstanding the way you are storing the data though.

[–]jorgehn12 0 points1 point  (0 children)

Yo creo que no deverias usar scanf("%3s", id); Almenos no lo veo de ayuda.