i am trying to enter string values (more than 1 for each array) for a1 and a2. I want to print them row by row but the program don’t print any of the values. How can i fix it?
#include<stdio.h>
int main (void){
int i;
char *a1[10], *a2[10];
for (i=0; i<10;i++){
printf("Enter text %d for a1", i);
scanf("%s",a1);
printf("Enter text %d for a2", i);
scanf("%s",a2);
}
for (i=0; i<10;i++){
printf("text %d is %s for a1",i,a1[i]);
printf("text %d is %s for a2",i,a2[i]);
}
return 0;
}
i tried to print elements of arrray row by row but i cant. Also the program doesn’t give an error. Program ending after entering the elements.
>Solution :
- Your program doesn’t compile as you never defined mecut.
- Use constants instead of hard coding magic values.
a1anda2are array of pointers. You need to allocate space for the strings you want to read in.- The two
scanf()overwrite data into the same variable but you want it be relative to the index. - When reading a string with
scanf()ensure you set the maximum string length to avoid buffer overflow. Consider usingfgets(). - Check the return value of
scanf()otherwise your variables might not be initialized. - For readability use
\nin yourprintf().
Here is working code:
#include <stdio.h>
#define N 10
#define STR_LEN 99
#define STR(s) STR2(s)
#define STR2(s) #s
int main (void){
char a1[N][STR_LEN+1];
char a2[N][STR_LEN+1];
for (int i=0; i<N; i++){
printf("Enter text %d for a1: ", i);
if(scanf("%" STR(STR_LEN) "s", &a1[i]) != 1) {
printf("scanf failed\n");
return 1;
}
printf("Enter text %d for a1: ", i);
if(scanf("%" STR(STR_LEN) "s", &a2[i]) != 1) {
printf("scanf failed\n");
return 1;
}
}
for (int i=0; i<N; i++) {
printf("text %d is %s for a1\n",i, a1[i]);
printf("text %d is %s for a2\n",i, a2[i]);
}
}
and example run:
$ seq 20 | ./a.out
Enter text 0 for a1: Enter text 0 for a2: Enter text 1 for a1: Enter text 1 for a2: Enter text 2 for a1: Enter text 2 for a2: Enter text 3 for a1: Enter text 3 for a2: Enter text 4 for a1: Enter text 4 for a2: Enter text 5 for a1: Enter text 5 for a2: Enter text 6 for a1: Enter text 6 for a2: Enter text 7 for a1: Enter text 7 for a2: Enter text 8 for a1: Enter text 8 for a2: Enter text 9 for a1: Enter text 9 for a2: text 0 is 1 for a1
text 0 is 2 for a2
text 1 is 3 for a1
text 1 is 4 for a2
text 2 is 5 for a1
text 2 is 6 for a2
text 3 is 7 for a1
text 3 is 8 for a2
text 4 is 9 for a1
text 4 is 10 for a2
text 5 is 11 for a1
text 5 is 12 for a2
text 6 is 13 for a1
text 6 is 14 for a2
text 7 is 15 for a1
text 7 is 16 for a2
text 8 is 17 for a1
text 8 is 18 for a2
text 9 is 19 for a1
text 9 is 20 for a2
Here is a refactored version that uses a couple of functions to reduce duplication. Also using fgets() instead of scanf() as you may not like how the latter by default reads words opposed to lines:
#include <stdio.h>
#include <string.h>
#define N 10
#define STR_LEN 100
int prompt_str(int index, const char *name, size_t len, char s[len]) {
printf("Enter text %d for %s: ", index, name);
if(!fgets(s, len, stdin)) {;
return 1;
};
s[strcspn(s, "\n")] = '\0';
return 0;
}
void print_str(int index, const char *name, const char *s) {
printf("text %d is %s for %s\n", index, s, name);
}
int main (void){
char a1[N][STR_LEN];
char a2[N][STR_LEN];
for (int i=0; i<N; i++){
if(
prompt_str(i, "a1", STR_LEN, a1[i]) ||
prompt_str(i, "a2", STR_LEN, a2[i])
)
return 1;
}
for (int i=0; i<N; i++) {
print_str(i, "a1", a1[i]);
print_str(i, "a2", a2[i]);
}
}