Hello, C newbie here...

below is a driver for a pretty simple function which is supposed to read in n chars from input and store in a char array.

#include <stdio.h>
#define SIZE 81

void get_str (char * array, int n) ;

int main (void)
{
	char instr [SIZE] ;
	printf ("Please enter string for storage:\n") ;
	get_str (instr, SIZE) ;
	printf ("The first %d characters of the string entered were:\n", SIZE) ;
	puts (instr) ;

	return 0 ;
}

void get_str (char * array, int n)
{
	int count ;
	char ch ;
	for (count = 0; count < SIZE && (ch = getchar()); count++)
		*(array + count) = ch ;
}

I would have expected that this code wouldn't work, because there is no null character stored at the end of the array. However, I tried compiling and running, and it works fine...not that I'm complaining, but why is there no null character needed?

Cheers:)

what makes you think your program is working? The loop on line 21 doesn't stop when you press the <Enter> key. And if you just type in a bunch of crap you get this:

Please enter string for storage:
lkaslkjas;lkfjaslkfjas;lkjfg;lkjglkjerpoitjergokjjgk;lkeo;ijtoijtrhoijrt;hoijrth
oijtgoijegoijewgoiejgoierjgpoijergoijergoijwegpoijwergpoiwjergpoiwejg
The first 81 characters of the string entered were:
lkaslkjas;lkfjaslkfjas;lkjfg;lkjglkjerpoitjergokjjgk;lkeo;ijtoijtrhoijrt;hoijrth
o╠╠╠╠╠╠╠óÇÇæ|■1
Press any key to continue . . .

Notice the junk stuff at the bottom just before "Press any key ...". That's caused by the string not being null terminated.

>why is there no null character needed?
Dumb luck. The next byte after your array just happens to have the value 0.

>*(array + count) = ch ;
Just out of curiosity, why add the unnecessary complexity of pointer notation here?

>*(array + count) = ch ;
Just out of curiosity, why add the unnecessary complexity of pointer notation here?

Is it really that much complex from array[count] that it would warrant wondering?
Separate question:
Doesn't the compiler default to substitute array notation for pointer notation?

>Is it really that much complex from array[count] that it would warrant wondering?
Yes. Pointer notation for subscripting is unconventional enough to raise red flags. When I see this I wonder if it's a requirement for a homework assignment or if the author is trying to look smarter by using "tougher" looking code. If it's the former I'll write it off as more classroom silliness. If it's the latter, I'll need to be on the lookout for macho code.

Either way, pointer notation is more complicated (if only a little), harder to read (if only a little), causes readers to question the code, and doesn't describe the author's intentions very well.

>Doesn't the compiler default to substitute array notation for pointer notation?
Array notation is syntactic sugar for pointer notation, yes.

>If it's the former I'll write it off as more classroom silliness. If it's the latter, I'll need to be on the lookout for macho code.

Oh, never saw it under that light. Always I have favored pointer notation when dealing with string pointers, if for any other reason that to make a feeble attempt for distinction between pointer and array.

>I'll need to be on the lookout for macho code.
Can't help but draw a parallel thought of six plus feet men making hulk poses for just placing a ball inside an over-sized metal ring.

>Always I have favored pointer notation when dealing with string pointers
If you're just using increment and decrement, there's no problem. But if you're already bringing an offset into the mix, there's not really a good reason to avoid the simpler and better understood syntax.

>Can't help but draw a parallel thought of six plus feet men making
>hulk poses for just placing a ball inside an over-sized metal ring.
It's almost like that, actually. ;)

>why is there no null character needed?
Dumb luck. The next byte after your array just happens to have the value 0.

Thanks, that explains it. I've modified the code now to add in a null for the last character.

>*(array + count) = ch ;
Just out of curiosity, why add the unnecessary complexity of pointer notation here?

I hadn't really thought about it to tell you the truth, I guess I didn't really see it as any more complex than array notation, although I probably would have seen it in a different light if I was using a multidimensional array. Certainly not machismo anyway :P
Point taken about readability though, thanks for your help :)

This question has already been answered. Start a new discussion instead.