0

The first program below is utilizing a pointer for the array movie:

#include <stdio.h>

int main(void) {
    char movie [20];
    char * pMovie = movie;

    fgets(pMovie, 20, stdin); 
    puts(pMovie);

    return 0;
}

While the second program below is not utilizing any pointer and essentially achieves the same outcome. I am not understanding the benefit that the teacher is trying to show with using pointers within the pointer but I can assume that it goes beyond this particular example:

#include <stdio.h>

int main(void) {
    char movie [20];

    fgets(movie, 20, stdin);
    puts(movie);

    return 0;
}
SiggiSv
  • 1,219
  • 1
  • 10
  • 20
Wunderbread
  • 898
  • 2
  • 14
  • 34
  • 1
    There's no technical benefit. The programs are equivalent. Arrays convert to pointers to their first character implicitly in contexts such as these (i.e., function calls). The explicit pointer is unecessary. – Petr Skocik Jun 04 '17 at 21:56
  • Teacher is showing you baby steps simple usage. – Weather Vane Jun 04 '17 at 21:56
  • 5
    There is a benefit (when **not** using a pointer) : `fgets(movie, sizeof movie, stdin);` will avoid dependency on the literal `20` – wildplasser Jun 04 '17 at 22:20

1 Answers1

-1

There is absolutely no benefit, because both things are very similar. In C an array can be considered as the pointer to the first element of the array (Inside of the scope of the function where the array is declared the compiler is also aware of the size of the array, so there is some different behavior between the 2, although not in your examples). A char array is a pointer to the first element in the array, notice how there is no type casting when he does:

char * pMovie = movie;

There is no need to type cast because these 2 variables are considered to be the same type (char *) by the compiler.

A similar way to create an array is:

char *test = (char*)malloc(sizeof(char)*size_of_array);

I am assuming your teacher is trying to make you familiar with this concept (that arrays in C are similar to pointers and vice-versa)

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • To whomever downvoted, please tell me why, what did I do wrong. Stop doing this, help me improve :C – Makogan Jun 04 '17 at 22:26
  • It's not my down-vote, but your second sentence "In C, an array _is_ a pointer" is flat-out wrong. All else apart, given `int a[32]; int *b;` it is clear that `sizeof(a) != sizeof(b)`, so an array is _not_ a pointer. Also, you can do `b = &a[0];` (assign a value to a pointer), but you can't do `int c[20]; a = c;` (you can't assign to an array). So, while your opening sentence is good and accurate, your continuation is not. An array name 'decays' into a pointer to the first (or zeroth, depending on how you count) element of the array in many contexts, but that doesn't make it into a pointer. – Jonathan Leffler Jun 04 '17 at 22:38
  • @Fredrik 'an is the same'? You're missing a word which you can edit into the comment if you're quick. – Jonathan Leffler Jun 04 '17 at 22:39
  • Should I delete this? – Makogan Jun 04 '17 at 22:42
  • Better, but still room for improvement. I'm tempted to agree with your 'delete this' suggestion. – Jonathan Leffler Jun 04 '17 at 23:03
  • [don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Jun 05 '17 at 13:21
  • why would you write that monstrosity instead of `char *test = malloc(size_of_array);` – M.M Jun 05 '17 at 13:49
  • @M.M to avoid compiler warnings, also just size of array is not enough, it needs to be size of array*size of the elements – Makogan Jun 05 '17 at 20:44
  • `char` has size 1, and if you get a compiler warning for this code it means you have an error in your program (e.g. you forgot to include `stdlib.h` and you're operating your compiler in 1980s mode) – M.M Jun 05 '17 at 20:56