1

It is printing correctly but I have confusion that whenever I write just msg, it gives me Your ?@ and whenever I write msg[option-1], it gives me full message of Your name is bilal. I am not understanding the cause of [option-1]. Why it is used and what is it's function?

#include <stdio.h>    
#define MAX_LEN 256

int main(){
  FILE * fp = fopen("file.txt","r");
  int option;
  char word[MAX_LEN];
  static const char * const msg[] = {
    "Name",
    "Date of Birth",
    "ID Card Number",
    "Phone Number",
    "Address",
    "Account", 
    "Fixing Year",
    "Amount" };
      for (option = 1; option <= sizeof(msg)/sizeof(char *); ++option)
      printf("%d. Your %s:\n", option, msg[option-1]);
  fclose(fp);
  return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Bilal Khan
  • 119
  • 1
  • 9
  • Sorry bro your code is giving me segmentation fault – Bilal Khan May 29 '20 at 10:53
  • The `%s` format expects a single string (of type `char *`). The array `msg` is an array of strings, it's type is `const char * const[8]`. Why would you expect to be able to use the array `msg` for the `%s` format? – Some programmer dude May 29 '20 at 10:56
  • The code posted works correct. Vlad explains why. – Paul Ogilvie May 29 '20 at 11:05
  • Ergo debatable way to use `for(option=1;option<=sizeof(msg)/sizeof(char *);++option)`, instead of `for(option=0;option<=sizeof(msg)/sizeof(char *)-1;++option)`, where the expression here is clearly a constant value computed at compile time, and introducing the term `option-1` in the loop that eventually could not be optimized on not so smart compilers. – Frankie_C May 29 '20 at 11:22

1 Answers1

0

The conversion specifier %s is designed to output strings. It expects an argument of the type char *.

The array msg is declared like

static const char * const msg[] = {
//...

that is its elements have the type char *. The array itself used in expressions has the type char **. So it may be supplied to the conversion specifier %s.

The valid range of indices to access elements of the array is [ 0, sizeod( msg ) / sizeof( char * ) ) while in the loop shown below the index variable is changed from [1, sizeof( msg ) / sizeod( char * ) + 1 ).

That is in this loop

  for (option = 1; option <= sizeof(msg)/sizeof(char *); ++option)

indices start fro 1. So to output correctly an element of the array you have to use the expression option - 1 as an index and the expression msg[option-1] has the required type char * that is expected by the conversion specifier of the call of prontf.

  printf("%d. Your %s:\n", option, msg[option-1]);

That is the selected from the array string is outputted.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I still didn't understand the [option-1] – Bilal Khan May 29 '20 at 11:25
  • @natashaBibi The first element of the array msg has the index 0. Is it clear? How to output this element if the variable option used as an index starts from 1? Or how to output the last element of the array if the variable option is equal to the size of the array? I showed you teh valie range of iondices. – Vlad from Moscow May 29 '20 at 11:27
  • So in my understanding is it working like this [option-1] == [8-1] then [7-1] and this repeats until it reaches the first one. Means it is going in reverse. – Bilal Khan May 29 '20 at 11:38
  • @natashaBibi You array has 8 elements. The valid indices to access the elements are 0-7 inclusively. The variable option starts from the initial value equal to 1 and is changed up to 8. So to output the first element or the last of the array with the index 0 you have to write msg[oprion-1]. – Vlad from Moscow May 29 '20 at 11:41
  • I understood the first element but what do you mean by the last of the array with the index zero. Is it on reverse? – Bilal Khan May 29 '20 at 11:45
  • @natashaBibi The last value of option used in the loop is equal to teh size of the array that is equal to 8 due to using the relational operator <= in the expression option <= sizeof(msg)/sizeof(char *). There is no element with the index 8 in tehharray. The valid range iof indices is 0-7. So to output the whole array you have to use the expression option - 1. In this case it will be changed from 0 to 7 as it is required. – Vlad from Moscow May 29 '20 at 11:52
  • option starts from 1, option = 1 so in a loop from 0-7, [option-1] will be like this 1-1=0 then 2-1=1 then 3-1=0 then go until 8-1=7 right? – Bilal Khan May 29 '20 at 11:59