-1
#include<stdio.h>
#include<stdlib.h>

struct keyVal
{
    int key;
    int val;
};

int main()
{

    struct keyVal *arr[5];
    int i;
    for(i=0;i<5;i++)
        printf("\n : %p     %p      ",&arr[i][0].val,&arr[i]);
    printf("\n\n");
    printf("\n : %d     %d      ",arr[0][0].val,arr[0]->val);
    printf("\n\n");

    for(i=0;i<5;i++)
        printf("\n : %d     %d      ",arr[i][0].val,arr[i]->val);
    printf("\n\n");

    return 0;
}

First, for( ; ; ); will generate same %p %p from arr[0][0] to arr[4][0] and arr[0] to arr[4], which means arr[i][0] == arr[i] where i = 0,1,2,3,4.

Second for( ; ; ); should print values(garbage value) of arr[i][0].key (arr[i]->key).

We can access key by doing:

arr[i][0].key OR arr[i]->key where i = 0,1,2,3,4.

Kcvin
  • 5,073
  • 2
  • 32
  • 54
ryan
  • 21
  • 3
  • What are the compiler warnings? – Sourav Ghosh Sep 14 '16 at 14:33
  • 2
    `arr` is not initialized, so the whole code is [UB](https://en.wikipedia.org/wiki/Undefined_behavior). – LPs Sep 14 '16 at 14:33
  • Could you reformat your question a little? As presented the code is very unclear. – kfb Sep 14 '16 at 14:34
  • @SouravGhosh run time error is : Segmentation fault (core dumped) – ryan Sep 14 '16 at 14:37
  • @ryan I'm almost sure your compiler warned you, but you chose to ignore them, or silenced your compiler.. :) – Sourav Ghosh Sep 14 '16 at 14:39
  • `printf("%p", ptr)` is one of the rare cases where, in C, you actually **do** need to cast the pointer: `printf("%p", (void *) ptr);` – Elias Van Ootegem Sep 14 '16 at 14:39
  • @SouravGhosh no warning at all. running on linux 64bit. – ryan Sep 14 '16 at 14:42
  • Related (not duplicate because linked article is about writing while this article is about reading): [c - Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to an uninitialized pointer? - Stack Overflow](http://stackoverflow.com/questions/37549594/why-do-i-get-a-mysterious-crash-or-segmentation-fault-when-i-copy-scan-data-to) – MikeCAT Sep 14 '16 at 14:51
  • 1
    Just a tip: put the `newline` at the *end* of the format string, not at the start. Then when you crash, there are no unprinted messages buffered, to confuse you as to where the program reached. You should only omit the final newline, if you have more to print in another statement, such as array values in a loop. Then, after the loop `printf("\n");` – Weather Vane Sep 14 '16 at 14:57
  • Please format your code correctly. – Jabberwocky Sep 14 '16 at 15:07

3 Answers3

0

You took 5 pointer variable pointing to keyVal struct for which memory is not allocated thats why it ran into segmentation fault. Use malloc to allocate memory and then use.

0

You can use &arr[i].val rather than &arr[i][0].val .

0

See the comments for explanation.
I left the last for {...} on purpose I'm hoping you will be able to correct it yourself

#include<stdio.h> 
#include<stdlib.h>

struct keyVal {
    int key;
    int val;
};

int main()
{
    /*
      You are creating an array composed of 5 cells.
      Each cell is bound to contain an address of a variable
      of which should be a keyVal.
    */
    struct keyVal *arr[5];
    int i;

    /* 
     Use curly brackets to wrap the content of the loop
     even though you only have one instruction it helps for readability
     and prevents errors.
    */
    for(i = 0; i < 5; i++) {
        printf(": %p | %p\n", &arr[i][0].val, &arr[i]);
    }

    /*
     You are getting a segmentation fault here because you are trying to access something which does not exist.
    */
    //printf(": %d - %d\n", arr[0][0].val, arr[0]->val);

    /***************************************************
     BEGIN CORRECTION :the field val will be initialized.
    ***************************************************/

    arr[0] = malloc(sizeof(struct keyVal)); // Allocating "sizeof(struct keyVal)" bytes in memory.
    arr[0]->val = 42; // Assigning a value to the field val in the first cell of arr. 
    printf(": %d | %d\n", arr[0][0].val, arr[0]->val); // Now we can print as a value now exists.

    /***************************************************
     END CORRECTION
    ***************************************************/
    /*
     Again use brackets ;p
     I think you can solve that one on your own, now.
    */  
    for(i = 0; i < 5; i++) {
        printf(": %d | %d \n", arr[i][0].val, arr[i]->val);
    }
    // Don't forget to free "ALL" the memory allocated with malloc.
    free(arr[0]);

    return 0;
}

Find out about malloc and free.

Remy J
  • 709
  • 1
  • 7
  • 18