0

What I need very precisely is an array A[10] and each of its element pointing to the respective element of array B[10] whose each element store its index.

Hence, A[1] points to B[1] and B[1] has value of 1. So, when I call *A[1] or *B[1], I get 1.

I know it can be super easy if the array B[10] is not an array of pointers but of integers but I need this for another purpose.

This is what I did but segmentation fault was offered.

#include <stdio.h>

int main() {
    int *A[10];
    int *B[10];
    
    for(int i=0; i<10; i++) {
        A[i] = B[i];
        *B[i] = i;
        printf("\n%d %d",*A[i],*B[i]);
    }
}

By the way, I am not very proficient in pointers.

9
  • Cannot reproduce, please provide a minimal reproducible example Commented Jun 9, 2021 at 7:03
  • @mr.loop your code works fine. Provide complete code that can be compiled. Commented Jun 9, 2021 at 7:06
  • x = *y[1]; is undefined behavior. You are de-referencing an uninitialized pointer. Commented Jun 9, 2021 at 7:27
  • @Jabberwocky My bad that I messed up the question. Edited finally to make it precise and reproducible. Commented Jun 9, 2021 at 17:14
  • @Lundin Edited to reflect the original question. Commented Jun 9, 2021 at 17:15

1 Answer 1

3

Your commented code :

int main() {
    int *A[10];   // an array of 10 pointers, each of them pointing nowhere
    int *B[10];   // an array of 10 pointers, each of them pointing nowhere

    // now each array a and b contain 10 uninitialized pointers,
    // they contain ideterminate values and they point nowhere
    
    for(int i=0; i<10; i++) {
        A[i] = B[i];     // copy an uninitialized pointer
                         // this usually works but it's pointless

        *B[i] = i;       // you assign i to the int pointed by *B[i]
                         // but as *B[i] points nowhere you end up with a segfault

        printf("\n%d %d",*A[i],*B[i]);  // you never get here because the previous
                                        // line terminates the program with a segfault,
                                        // but you'd get a segfault here too for 
                                        // the same reason
    }
}

Your program is basically equivalent to this:

int main() {
    int *a;     // a is not initialized, it points nowhere
    *a = 1;     // probably you'll get a segfault here
}

Accessing the thing pointed by a pointer is called dereferencing the pointer. Dereferencing an uninitialized pointer results in undefined behaviour (google that term), most likely you'll get a seg fault.

I'm not sure what you're trying to achieve, but you probably want something like this:

#include <stdio.h>

int main() {
  int* A[10];
  int B[10];

  for (int i = 0; i < 10; i++) {
    A[i] = &B[i];
    B[i] = i;
    printf("%d %d\n", *A[i], B[i]);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is correct. But is there a way to do this using pointer array rather than int array. Its because some of the elements of B have to store index and other point somewhere.
@mr.loop a way to do what? This is an XY Problem. You should tell us what you are actually trying to do by asking an other question. The problem with your code is that the pointers in the second array point nowhere.

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.