ItsASecret
ItsASecret

Reputation: 2649

How can I test this C function

I got a weird question to do as an exercise :

Write a function which take a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of an int as a parameter and assign a value to it.

I think the function I wrote is right (please correct if it's not) but how can I test it ?

void function(int *********anInt)
{
    *********anInt = 5;
}

I tried :

int main(void) {
    int *********nbr = malloc(sizeof(int));
    function(nbr);
    printf("%d", *********nbr);
}

But I get a segfault, I just learned about malloc and pointers so I don't fully understand it.

Upvotes: 5

Views: 2167

Answers (5)

Jonathan Leffler
Jonathan Leffler

Reputation: 753665

You'll need a ridiculous main program to go with the assignment from hell!

int main(void)
{
    int         l0 = 0;
    int        *l1 = &l0;
    int       **l2 = &l1;
    int      ***l3 = &l2;
    int     ****l4 = &l3;
    int    *****l5 = &l4;
    int   ******l6 = &l5;
    int  *******l7 = &l6;
    int ********l8 = &l7;

    printf("%d %d %d %d %d %d %d %d %d\n", l0, *l1, **l2, ***l3, ****l4, *****l5,
           ******l6, *******l7, ********l8);
    function(&l8);
    printf("%d %d %d %d %d %d %d %d %d\n", l0, *l1, **l2, ***l3, ****l4, *****l5,
           ******l6, *******l7, ********l8);
    return 0;
}

Untested: maybe I didn't count something right, but the general idea is about correct. This is a torture test — for innocent C programmers and for compilers.

Upvotes: 5

Sergey L.
Sergey L.

Reputation: 22542

Try

int main() {
    int *********nbr;

            nbr = malloc(sizeof(int********));
           *nbr = malloc(sizeof(int*******));
          **nbr = malloc(sizeof(int******));
         ***nbr = malloc(sizeof(int*****));
        ****nbr = malloc(sizeof(int****));
       *****nbr = malloc(sizeof(int***));
      ******nbr = malloc(sizeof(int**));
     *******nbr = malloc(sizeof(int*));
    ********nbr = malloc(sizeof(int));
    function(nbr);
    printf("%d", *********nbr);
}

Upvotes: 7

Don
Don

Reputation: 4157

See md5's solution, however it lacks explaination

Explained:

The reason your test program didn't work is because malloc returns a void* which is simply a memory address (a pointer). You assigned this to an int*****... which means when the program tries to dereference down to the actual int what it's doing is first taking the memory address of the int and dereferencing it (which is okay) but after this since your value (5) is now the value it then derefences that, which should come back with your segfault.

Think of the assignment as nested dereferences:

int ********p8 = *anInt; // p8 == 5
int *******p7 = *p8; // This breaks since dereferencing memory
                     // address 5 results in a segfault

What was done to avoid this was we actually nested the pointers that way when dereferencing for assignment we have memory addresses (pointers) to dereference to eventually get to the memory address which stores the value.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

An int** is a pointer that points to a pointer:

int myInt;
int* pInt = &myInt;
int** ppInt = &pInt;

An int*** is a pointer that points to a pointer that points to a pointer:

int*** pppInt = &ppInt;

To test your function, you need to carry this on and on, the right number of times.

Upvotes: 2

md5
md5

Reputation: 23699

Of course, you can test it, although it looks weird.

#include <stdio.h>

void function(int *********anInt)
{
    *********anInt = 5;
}

int main()
{
    int n = 0;
    int *p1 = &n;
    int **p2 = &p1;
    int ***p3 = &p2;
    int ****p4 = &p3;
    int *****p5 = &p4;
    int ******p6 = &p5;
    int *******p7 = &p6;
    int ********p8 = &p7;
    function(&p8);
    printf("%d\n", n);
    return 0;
}

Upvotes: 18

Related Questions