-1

this is my code:

void n(FILE *f, int *pocet, char **spz) {
    int i = 0;
    int pocett = 0;
    char* r = (char*)malloc(50 * sizeof(char));
    if (spz != NULL) {
        free(spz);
    }
    spz = (char**)malloc(sizeof(char*));
    if (f == NULL) {
        printf_s("File is null");
        return;
    }
    while (fgets(r, 50, f)) {
        switch (i % 6) {
        case 1: {
            if (i == 1) {
                spz[0] = (char*)malloc(50 * sizeof(char));
                spz[0] = r;
            }
            else {
                spz = (char**) realloc(spz,((i - 1) / 6 + 1) * sizeof(char*));
                spz[((i - 1) / 6)] = (char*)malloc(50 * sizeof(char));
                strcpy_s(spz[(i-1)/6], 50 ,r);
            }
            break;
                }
        case 5: {
            pocett++;
            break;
                }
        }
        i++;
    }
    *pocet = ++pocett;
}

and I call my function as this:

int main() {
    FILE *f = NULL;
    int c;
    int pocet = 0;
    int* p  = &pocet;
    char** spz = NULL;
    while ((c = getchar()) != 'k') {
        getchar();
        switch (c) {
            case 'v': {
                v(&f);
                break;
                      }
            case 'o': {
                //printf_s("AHOJ\n");
                break;
                      }
            case 'n': {
                n(f, p, spz);
                break;
                      }
            case 's': {
                s(spz, *p);
                break;
                      }
            case 'p': {
                printf_s("AHOJ\n");
                break;
                      }
            case 'z': {
                printf_s("AHOJ\n");
                break;
                      }
            default: {
                printf_s("Skus znova\n");
                break; 
                     }
        }
    }
    return 0;
}

EDIT: I have edited my code to add main method. the problem is, when I pass the spz to another method after, it is null, that means I have to use it in method n() as triple pointer, but it is not working when I add asterisk before each spz in method n. Do you have any ideas how to fix this ?

CiaPan
  • 9,381
  • 2
  • 21
  • 35
Lukas Anda
  • 716
  • 1
  • 9
  • 30
  • Please [edit] your question and use either C or C++ tag. Languages are not the same, so pick one you are using. – user694733 Nov 09 '17 at 08:41
  • 2
    I think you have a serious misconception about pointers. Please [edit] your question and show how you call the `n` function. (BTW: `n` as a function name is a pretty poor choice). – Jabberwocky Nov 09 '17 at 08:42
  • 1
    @Traabefi The function does not make sense at least due to these statements spz[0] = (char*)malloc(50 * sizeof(char)); spz[0] = r; So there is nothing to discuss. – Vlad from Moscow Nov 09 '17 at 08:45
  • 1
    Please [edit] your post and provide [mcve]. At the moment your code has stuff we don't need to see, and is missing stuff that we need to see. – user694733 Nov 09 '17 at 08:45
  • The only effect your function has on spz is freeing it. Memory allocated and assigned to spz is lost (leaked). This version thus cannot possibly work. If your function with added asterisks doesn't work, show the version with added asterisks. – n. m. could be an AI Nov 09 '17 at 08:49
  • 1
    @Traabefi The pointer char** spz = NULL; is NULL because it is not changed in main. – Vlad from Moscow Nov 09 '17 at 08:50
  • 1
    When you pass a pointer to the function, the function receives **a copy of** the pointer -- with its very own, and very different address than it had in the calling function (e.g. `main()`). To pass an unallocated pointer to a function, and have the contents available back in `main()` you must pass the **address of** the pointer, (e.g. `n(f, p, &spz);` (becoming a 3-star programmer -- not a compliment) Better to declare `char **n (...)` and return a pointer to the newly allocated block. – David C. Rankin Nov 09 '17 at 08:50
  • Re your question edit: make use of the function's return value. – Weather Vane Nov 09 '17 at 08:53
  • I have to do it without return value, for my school project – Lukas Anda Nov 09 '17 at 08:55
  • Please tell us what the program is supposed to do. Then it will be easier for us to help. – Jabberwocky Nov 09 '17 at 08:56
  • Since `i = 0;` in `n`, your `i % 6` is always `0` in `switch (i % 6)` no `case:` in your `switch` statement ever matches. – David C. Rankin Nov 09 '17 at 09:01

1 Answers1

1

When you call a function you pass a copy of the spz variable from the main() function to an spz variable in the n() function. Then, assignment in n() affects only its local spz.

If you want to get a pointer value back, you need to pass a pointer to the variable and the routine must dereference the pointer to reach the original variable:

void n( char ***spz_ptr)    // a pointer to (char **) variable
{
    *spz_ptr                // dereferenced pointer. i.e. the main's spz
        = malloc( ... );
}

void main()
{
    char **spz;

    n( & spz );             // pass a pointer to spz
    if( spz == NULL) {      // test the value assigned by n()
        ....  // handle the error
    }
    else {
        ....  // proceed with actual work
    }
}
CiaPan
  • 9,381
  • 2
  • 21
  • 35