2

I've made a test, and I tried to pass a number to an int pointer like this:

void   mega_pointer(int *********number)
{
    *********number = 1337;
}

But I have a segmentation fault. I don't understand. Can somebody help me ?

EDIT : I add my main :

int   main(void)
{
   int ********number;

   mega_pointer(&number);
   printf("Result = %d\n", ********number);
   return 0;
}

PS : Sorry for your eye, I will never use 9* in a real program, it's just an experiment ^^

7
  • 7
    How do you call this function? Commented Aug 15, 2013 at 10:40
  • 1
    For simplicity sake, a pointer should point to a valid memory location before getting dereferenced. Check in your case if number is pointing to valid memory location. BTW, so many stars burnt my eyes. Commented Aug 15, 2013 at 10:42
  • Wow, that level of indirection would only exist in hell :) Commented Aug 15, 2013 at 10:42
  • @MiklósHomolya christmassy function ? somehow it reminds me of that part of the year. Commented Aug 15, 2013 at 10:43
  • Double pointers are enough in C++. How one could able to create that scenario... Commented Aug 15, 2013 at 10:45

5 Answers 5

5

The important question is how do you call the function; it needs a 9 indirection chain to be called successfully. See the example

#include <stdio.h>

void mega_pointer(int *********number)
{
    *********number = 1337;
}

int main() {
    int i = 42;
    int *p1 = &i;
    int **p2 = &p1;
    int ***p3 = &p2;
    int ****p4 = &p3;
    int *****p5 = &p4;
    int ******p6 = &p5;
    int *******p7 = &p6;
    int ********p8 = &p7;
    int *********p9 = &p8;
    mega_pointer(p9);
    printf("%d\n", i);
}

prints 1337

Furthermore there is error in your program, as indeed your code is wrong, and you are assigning 1337 to an lvalue of type int * (in my code this would be p1 instead of i; this however goes unnoticed if warnings are not enabled. If you compile your original code with gcc -Wall, then you get warning: assignment makes pointer from integer without a cast [enabled by default].

Sign up to request clarification or add additional context in comments.

Comments

4

You don't have as many * characters in front of the assignment as you do in front of the parameter. It should be:

void mega_pointer(int *********number)
{
    *********number = 1337;
}

In the case that you have the right number of those in the program, it should work fine. You just have to make sure you have valid variables which you are pointing to. For example this program works:

#include <stdio.h>

void mega_pointer(int *********number)
{
    printf("%d\n", *********number);
    *********number = 1337;
    printf("%d\n", *********number);
}

int main()
{
    int blah = 1;
    int *blah1 = &blah;
    int **blah2 = &blah1;
    int ***blah3 = &blah2;
    int ****blah4 = &blah3;
    int *****blah5 = &blah4;
    int ******blah6 = &blah5;
    int *******blah7 = &blah6;
    int ********blah8 = &blah7;
    int *********blah9 = &blah8;

    mega_pointer(blah9);

    return 0;
}

It prints:

1
1337

As expected.

2 Comments

i think number points to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer to an int.
Sorry, I forget a * here, but the number of * is right in my program (otherwise it would not compile I think)
1

Your code works, but you have to set-up the pointers correctly first:

#include <stdio.h>

static void mega_pointer(int *********number)
{
    *********number = 1337;
}

int main(int argc, char **argv) {
    int a = 0;
    int *p1, **p2, ***p3, ****p4, *****p5, ******p6, *******p7, ********p8, *********p9;

    p1 = &a;
    p2 = &p1;
    p3 = &p2;
    p4 = &p3;
    p5 = &p4;
    p6 = &p5;
    p7 = &p6;
    p8 = &p7;
    p9 = &p8;

    mega_pointer(p9);

    printf("a=%d\n", a);
    return 0;
}

When run:

$ clang -o p ptest.c
$ ./p
a=1337

Comments

0

the number op "*" sign represent the number of pointing that the variable is - meaning it's a pointer to a pointer to a pointer....

you have 9 "*" in the function signature, but "only" 8 in the ********number = 1337;

if you'll do

void   mega_pointer(int *********number)
{
    *********number = 1337;
}

it'll work, but i would advise not to use that many pointing as it can be tricky (as you found out) and lead to truble

Comments

0

Segmentation fault? Segmentation fault is something happen when you try to access memory which is not associated with it, or may doesn't exist for program.

you can create this fault just by:

int *ptr = NULL;
*ptr = 1;

You are accessing a memory which is not associated to this program for now or may be in past.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.