Problems with strings - Tutorial 46 - C

0 Adrian Fab · April 13, 2015
Hi guys,

I wonder why Buckys used a pointer on his code when he could wrote it like this, and it works as well (and the same way):


int main(){
char movie [20];
fgets(movie, 20, stdin);

Could you help me? I didn't understand why he did use a pointer.

Post a Reply


Oldest  Newest  Rating
+1 c student · April 13, 2015
maybe because he thought he needed a char *string since the arguments are:
char *fgets (char *, int, FILE *);   /* first argument is char pointer */

but why not ask him?
0 Dol Lod · April 13, 2015
For accuracy, I would like to point out that all arrays are pointers. 

Additionally, I believe he used dynamically allocated memory b/c he wanted to avoid potentially having stack overflow. Otherwise, there would be huge security exploits. 
0   · April 14, 2015
I had to re-register because firstly, I don't even know why I was banned. It'd be nice to be told what I did wrong, so I can avoid doing that in the future.

Most importantly, for the sake of anyone reading this thread, here's some correct information...

We can declare and assign to a pointer variable over and over again... For example

char c, *p;

c = '\0';
p = NULL; // First pointer assignment
p = &c; // Second pointer assignment

We can't assign like this to arrays, however. Firstly, the value NULL is nothingness, so the moment you have an array allocated you have   something   which can't possibly be NULL. Secondly, you simply can't assign to an array using the = operator like this.

Do I need to continue to explain why "arrays are pointers" is incorrect? There are many, many more points.

If we declare a function as follows:

void foo(char **bar) {
/* It doesn't matter what goes in here */

... and call it twice, once with &pointer as the argument and once with &array as the argument, what do you think the compiler will say?

int main(void) {
char array[] = "bar",
*pointer = array;
foo(&pointer); // NO WARNING HERE
foo(&array); // WARNING HERE!!!

If arrays are pointers, then why does one line give no warning whilst the other gives "incompatible type" warning?

Let us declare a similar set of variables, except we'll declare array to have   2 * sizeof pointer   characters in it. All pointers of the same type have the same size, so if the array is a pointer then the array will have the same size as the pointer.


int main(void) {
char *pointer; // Notice how this is uninitialised? What do you suppose sizeof pointer might be?
char array[2 * sizeof pointer];
printf("sizeof array: %zu\n", sizeof array);
printf("sizeof pointer: %zu\n", sizeof pointer);

The concept of arrays intersects with the concept of pointers, but they aren't equal. The correct explanation is: Unless it's the subject of an &address-of or sizeof operator, an expression that is of type "array of T" will be silently converted to an expression that is of type "pointer to T".
Though heap overflows are more tedious to exploit than stack overflows, they're not still not impossible, so whether malloc is used or not makes little difference; an overflow here would still potentially produce vulnerable code.

In this particular case, it doesn't matter because the size of the argument matches the size that fgets is given. The only thing this code is vulnerable to is an uninitialised variable when the user presses CTRL+Z on Windows or CTRL+d on Linux.

You might also find my previous explanations interesting...

... here:

... and here:
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator