# array of strings

1 vote
239 views
#include <stdio.h>

int main(void) {
char* ar[4];
scanf("%s",ar[3]);
printf("%s",ar[3]);
return 0;
}

char *test1[3]= {"arrtest","ao", "123"};

if one can initialize array like this ,how   can input be taken by using scanf ?

char *ar[3]= {"arrtest","ao", "123"};

$\rightarrow$ Here $ar$ is an array of character pointers.

$\rightarrow$  In this each character pointer i.e. ar[0] , ar[1] and ar[2] is pointing to the first char of the string constants "arrtest", "ao" and  "123" respectively.

$\rightarrow$ When we execute the above line then first the string constants are created at a memory location and then the pointers start pointing to memory location of the first character of each of the string constants.

char* ar[4];
scanf("%s",ar[3]);

This will not work. why ?

$\rightarrow$ Because first you are creating an array of character pointers and since they are not initialized so they are pointing to nothing.(i.e. not pointing to any memory location)

$\rightarrow$ So when you write $ar[3]$ in $scanf$ , $a[3]$ will not provide any address location to $scanf$. ($\because$ a[3] is not pointing to any memory location.)

$\rightarrow$So if $scanf$ takes the input then where will it store the input ?

$\rightarrow$ In order to correct it you should first dynamically create a memory location and make the pointer point to that memory location.

$\rightarrow$ So that when you take the input using $scanf$ then the value will be stored at that memory location which you created at which the pointer is pointing.

char* ar[3];
ar[0] = (char*) malloc(20); //char*  is doing typecasting.
ar[1] = (char*) malloc(20);
ar[2] = (char*) malloc(20);

scanf("%s",ar[0]);
scanf("%s",ar[1]);
scanf("%s",ar[2]);

printf("%s",ar[0]);
printf("%s",ar[1]);
printf("%s",ar[2]);

0

Cannot recognise function actually https://ideone.com/zO8aMW

0

@srestha

You are trying to overwrite a string constant which is not correct.

$ar[2]$ is pointing to the memory location where the string constant "123" is stored.

scanf("%s",ar[2]);

so this is trying to overwrite "123" which is a string constant with the input value.

0
no, I just decleared string inside array
0

no, I just decleared string inside array.

They are not string, they are string constants.

scanf("%s",ar[2]);

This line is trying to overwrite the string constant.

You have declared the array of pointers but have not assigned any space to it. You need to first assign space to every element in the array of pointers dynamically(use malloc) and then you can call scanf...

## Related questions

1
103 views
What is the number of terminal string of length <= 6 generated by the CFG shown below? S -> 0A1 A -> BB1/B B ->A/0/1 Do you have any semantic method to solve such questions based on some pattern that can generalizes up to any length string n ?? For the present question it is 6,quite simple to derive but I am wondering what if it had been asked no of strings of length n Thanks
Number of strings up to length $3$ on alphabet set $\sum$ = { a,b,c,d } are :(including,string of length zero)? $A) 16$ $B) 85$ $C) 128$ $D) 64$