I hope some of you may help me with a basic understanding of pointers assigned to an array with chars in it (specifically letters). My problem is that part of my code when the user is supposed to input letters, 'D' or 'U' doesn't work and I don't know exactly where the problem is. I was trying to set it without pointers but that made my program more complicated and I'm also trying to finally learn that goddamn thing.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
scanf("%d", &number);
char *pz;
char save[number];
for (int i = 0; i < number; i++)
{
pz = &save[i];
scanf("%s", pz+i);
}
It seems that instead of this
for (int i = 0; i < number; i++)
{
pz = &save[i];
scanf("%s", pz+i);
}
you mean
for (int i = 0; i < number; i++)
{
pz = save + i;
scanf( " %c", pz );
}
Pay attention to the blank in the format string " %c"
.
The pointer pz
already points to the desired position in the array
pz = save + i;
So using the expression pz+i
in the call of scanf
does not make a sense.
And instead of the conversion specifier %s
you have to use the conversion specifier %c
.
After inputting the character array you can output it like
printf( "%.*s\n", number, save );
Or you can output the array in a loop like for example
for ( pz = save; pz != save + number; ++pz )
{
putchar( *pz );
}
putchar( '\n' );