C: Access to the pointer from outside the function

I have the following code:

int takeEven(int *nums, int numelements, int *newlist) {
    newlist = malloc(numelements * sizeof *newlist);
    int i, found = 0;
    for(i = 0; i < numelements; ++i, nums++) {
        if (!(*nums % 2)) {
            *(newlist++) = *nums;
            found++;
        }
    }
    newlist -= found;
    printf("First number found %d\n", *newlist); // <= works correctly
    return found;

}

int main()
{
    int nums[] = {1,2,3,4,5};
    int *evenNums;
    int i;
    int n = takeEven(nums, sizeof(nums) / sizeof(*nums), evenNums);
    for (i = 0; i < n; ++i) {
        printf("%d\n", *(evenNums++));
    }
    return 0;
}

The output of the above code:

-1
2088999640
2088857728

If I try to print the first element of the pointer newlistbefore returning the function ( printf("First number found %d\n", *newlist);), it works as intended, but why is it when I try to access the pointer from outside the function that I get these values ​​from seemingly unmarked addresses?

+3
source share
5 answers

You need to pass a pointer to a pointer, i.e. int **newlist. In particular, the new list is passed to your function by value, so the new list in mainand inside your function are two completely different variables.

:

#include <stdio.h>
#include <stdlib.h>

int takeEven(int *nums, int numelements, int **newlist) {
    int *list = malloc(numelements * sizeof **newlist);
    *newlist = list;  // this modifies the value of newlist in main
    int i, found = 0;
    for(i = 0; i < numelements; ++i, nums++) {
        if ((*nums % 2) == 0) {
            *(list++) = *nums;
            found++;
        }
    }
    list -= found;
    printf("First number found %d\n", *list); // <= works correctly
    return found;
}

int main()
{
    int nums[] = {1,2,3,4,5};
    int *evenNums;
    int i;
    int n = takeEven(nums, sizeof(nums) / sizeof(*nums), &evenNums);
    for (i = 0; i < n; ++i) {
        printf("%d\n", *(evenNums++));
    }
    return 0;
}

C-FAQ, :

Q: , :

void f(int *ip)
{
    static int dummy = 5;
    ip = &dummy;
}

:

int *ip;
f(ip);

.

A: , , ? , C . . , , ( , ):

void f(ipp)
int **ipp;
{
    static int dummy = 5;
    *ipp = &dummy;
}

...

int *ip;
f(&ip);

- :

int *f()
{
    static int dummy = 5;
    return &dummy;
}

...

int *ip = f();

. 4.9 4.11.

+3

newList , . .

int takeEven(int *nums, int numelements, int **newlist) {
    *newlist = malloc(numelements * sizeof *newlist);
    ...
}

...

int n = takeEven(nums, sizeof(nums) / sizeof(*nums), &evenNums);
+6

, , .

, malloc ( ) , .

, , , .

int use_pointed_memory(char **pointer){
  *pointer = malloc();
}

char *myptr;
use_pointed_memory(&myptr);

, , , .

+2

:

int n = takeEven(nums, sizeof(nums) / sizeof(*nums), evenNums);

, . :

newlist = malloc(numelements * sizeof *newlist);

Since this is just a copy, the caller will not see the result of your assignment. What you apparently want here is to pass a pointer by reference - for this you need a pointer to a pointer:

int takeEven(int *nums, int numelements, int **newlist) {
    *newlist = malloc(numelements * sizeof **newlist); // apply * to newlist
    ...
}

int n = takeEven(nums, sizeof(nums) / sizeof(*nums), &evenNums);

And don't forget free:

free(evenNums);
+1
source

In C, everything is passed by value. So you pass a copy of the function evenNumsto the function. Everything that you change inside the function is not reflected on the outside. You need int**as the third parameter.

0
source

Source: https://habr.com/ru/post/1723085/


All Articles