0

I have this piece of code from a program i am writing that doesn't work if i add a condition. All the values are conveniently put based on those the original program would have. I want to create an array of pointers:

    #include <stdio.h>

    int main() {

        int *array2Ptr[3];
        int array2[3][5] = {{4,4,5,5,8},{2,8,2,3,6},{8,8,6,6,1}};
        
        for ( size_t i = 0 ; i < 3 ; ++i) {
             array2Ptr[i] = &array2[i][4];
        }
    
         for (size_t i = 0 ; i < 3 ; ++i) {  
             printf("%d  ", *array2Ptr[i]);   
         }
      }

No problem here, it works fine.

Now, suppose i want array2Ptr to contain its element based on another array:

    int array1[3] = {3,0,3};
    int array2[3][5] = {{4,4,5,5,8},{2,8,2,3,6},{8,8,6,6,1}};
    int *array2Ptr[2];    //Has size 2 because it should only store 2 values

    for ( size_t i = 0 ; i < 2 ; ++i) {

        if ( array1[i] != 0 ) {
           array2Ptr[i] = &array2[i][4];
        }
     }
    
     for (size_t i = 0 ; i < 2 ; ++i) {  
         printf("%d  ", *array2Ptr[i]);   
    
     }
   }

This way i get a segmentation fault, why is that?

3
  • array2Ptr has size 2, but you are writing array2Ptr[i] for i=2 (3rd item). You need a separate counter for that array. Commented Aug 19, 2022 at 17:27
  • @EdmCoff yes I corrected it, in the original it would have been 2, but i get the error still. Commented Aug 19, 2022 at 17:30
  • 1
    Okay, then you are trying to print an undefined reference. array2Ptr[1] has not been set when you try to print it. Commented Aug 19, 2022 at 17:32

2 Answers 2

2

array2Ptr has size 2, but you are writing array2Ptr[i] for i=2 (3rd item). Edit: In your updated/edited version, you are trying to print array2Ptr[i] when i=2, which was never set to anything (array1[i] != 0 was false).

You need a separate counter for that array.

You could consider something like:

for ( size_t i = 0, j = 0; i < 3 ; ++i) {
 if ( array1[i] != 0 ) {
  array2Ptr[j] = &array2[i][4];
  j++;
 }
}

That way, you only increment the iterator j for array2Ptr when it is used, but still use i as you currently are for the other arrays.

You might want to track that j outside the scope of the loop, so you know how many values to try printing.

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

Comments

0

It's a better idea to use another iterator like j but if you want to do this with only one iterator, here's a simple solution for that particular code:

for (i = 0 ; i < 2 ; ++i) {

    if ( array1[i] != 0 ) {
        array2Ptr[i] = &array2[i][4];
    }
    else {
        array2Ptr[i] = &array2[i+1][4];
    }
 }

2 Comments

Does this work in the general case? I agree it will work for this particular example, but if array1 were {3,0,0,0,3} it seems odd to write values you don't want into array2Ptr (e.g. array2Ptr[1] will be set with a value that doesn't meet the if condition. And if array1 ends in 0 (e.g. {3,3,0}) this will try to write array2[3] (i+1) which is out of range.
Yes it surely does not work for the general case, if array1 is something like you write, then we would increment i inside the else part or open new conditions and it surely would not be useful. I just wrote it for this particular example, based on the specific values of these arrays.

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.