Can you initialize and assign pointers in the same instance?
1) My question refers to "char *charPtr[ 3 ]". I guess it's a dumb question it seems obvious you can't. But is there anyway to assign pointers through using "{ }" at least?
Code:
/*
Write a program that inputs several lines of text and a search character and
uses function strchr to determine the total occurrences of the character in
the lines of text.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char string1[ 50 ] = { };
char string2[ 50 ] = { };
char string3[ 50 ] = { };
char character;
char *charPtr[ 3 ] = { string1, string2, string3 };
int counter = 0;
int i;
printf( "Enter 3 lines:\n" );
scanf( "%s", string1 );
scanf( "%s", string2 );
scanf( "%s", string3 );
printf( "Enter character: " );
character = getchar();
for (i = 0; i < 3; ++i) {
while ( strchr( charPtr[ i ], character ) ) {
++counter;
}
}
printf( "%d\n", counter );
return 0;
}
2) Also my getchar doesn't seem to be working?
Code:
root[~]# ./a.out
Enter 3 lines:
d
d
d
Enter character: 0
root[~]#
It enters 0 on its own.