-3

I'm trying to compile a program that sets a pointer to a pointer to pointer to a pointer to a pointer to a pointer to a pointer to a pointer to a pointer. (9 times)

I've tried removing chunks of code and adding putchar and printf instead of write. I also tried to include stdio.h library, but it wont compile. I still don't understand pointers very well.

#include<stdio.h> // Adding a library here

void ft_ultimate_ft(int *********nbr); // Defining the ultimate function

int main(void)
{
  *nbr [9], // Defining the array for all pointers 
    *nbr[0] = 42 // Defining the final destination
    *nbr[1] = &nbr[0]
    *nbr[2] = &nbr[1]
    *nbr[3] = &nbr[2]
    *nbr[4] = &nbr[3]
    *nbr[5] = &nbr[4]
    *nbr[6] = &nbr[5]
    *nbr[7] = &nbr[6]
    *nbr[8] = &nbr[7]

   write(1, *nbr[8], 1);
   return(0);
}

I expected to get simply a 42, but got this error code instead. ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • 1
    Your pointer types don't match (or, they probably won't once you fix the declaration of `nbr[]`). – Steve Summit Jul 12 '19 at 14:39
  • 1
    Perhaps it is time to get [a good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start with the basics, for instance with how a proper statement looks like and how to declare a variable. – Blaze Jul 12 '19 at 14:41
  • There is no practical use for a 9-level pointer (no C program on Earth uses that many levels of indirection), so unless this is some kind of academic exercise, I'm not sure how much point there is in getting this to work. – Steve Summit Jul 12 '19 at 14:42
  • If it were me, I'd do something along the lines of `int i = 42; int *p1 = &i; int **p2 = &p1; int ***p3 = &p2; printf("%d\n", ***p3);` – Steve Summit Jul 12 '19 at 14:44
  • I would expect to get some severe compilation errors. Way before the linker might be involved. You should first convert these lines of pseudo code into C. – Gerhardh Jul 12 '19 at 15:03
  • @SteveSummit Maybe it is meant to be some (rather strange) preparation for using linked lists. – Gerhardh Jul 12 '19 at 15:08
  • Your code doesn't even compile: `error: ‘nbr’ undeclared (first use in this function)` and `error: expected ‘;’ before ‘write’` – S.S. Anne Jul 12 '19 at 15:22

2 Answers2

3
#include <stdio.h>


void ft_ultimate_ft(int *********nbr)
{
    printf("The pointer is %p.\n", (void *) nbr);
    printf("The number is %d.\n", *********nbr);
}


int main(void)
{
    ft_ultimate_ft(
        &(int ********) {
         &(int *******) {
          &(int ******) {
           &(int *****) {
            &(int ****) {
             &(int ***) {
              &(int **) {
               &(int *) {
                &(int ) { 42 } } } } } } } } }
        );
}

Do not do this.

The above uses compound literals. The form of a compound literal is ( type ) { initializer-list }. A compound literal creates an object with no name other than its literal text in the source code. For example, int x = 42; creates an int object named x whose initial value is 42, but (int) { 42 } creates an int object with no name whose initial value is 42.

Having created an object, we can take its address with &, so &(int) { 42 } is the address of an int with value 42. Then we can use that as the initial value for a compound literal of type int *, and we can take its address and use that to make a compound literal of type int **, and so on.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Try something like this.:)

#include <stdio.h>
#include <stdint.h>

void f( void *p, size_t n )
{
    while ( n-- ) p = ( void * )*( intptr_t * )p;

    printf( "%li\n", *( intptr_t * )p );
}

int main( void )
{
    enum { N = 9 };
    intptr_t nbr[N];

    nbr[0] = 42;
    for ( size_t i = 1; i < N; i++ ) nbr[i] = ( intptr_t )&nbr[i-1]; 

    f( ( void * )nbr[N-1], N - 2 );
}

The program output is

42
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335