0

This may be a very basic question but the idea of having pointers in C seems confusing to me or may be I don't know the exact purpose. I will provide some examples to demonstrate what my concerns are:

1st point:

Definition says something like this:

A pointer is a variable containing the address of another variable.

So, if one program goes like this:

int i = 23;
printf("%d", i);
printf("%d", &i);

And, another program goes like this:

int i = 23;
int *ptr;  
ptr = &i;
printf("%d", *ptr);
printf("%d", ptr);

Both the programs above can output same thing.

If pointer also keeps the variable's address in it and at the same time we can get the variable's address using & sign, can't we do the same task pointer does by deriving the address of any variable? I mean if I don't declare it as pointer and use it as int ptr = &i; in 2nd code snippet and use it as normal variable, what would be the differences?

2nd point:

I found somewhere here that:

C does not have array variables....but this is really just working with pointers with an alternative syntax.

Is that statement correct? As I am still beginner, I can't validate any statement regarding this. But this was somewhat confusing to me. If that statement is correct either, then what is the actual workaround in this regard? Is it actually the pointers which works in back-end and just the compilers/ide are fooling us by using array (obviously for maintaining simplicity)?

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • 5
    Don't learn C out of the gutter. Get a good book. I started with K & R. Even though it's old it still teaches the basics really rather well. – Bathsheba Jun 26 '17 at 15:58
  • 4
    Here are some more book options: [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/445976) – Blastfurnace Jun 26 '17 at 16:02
  • Yes, as @Bathsheba said. A pointer is not a variable that stores the address of another variable, it can be. But that's not what it is. – Iharob Al Asimi Jun 26 '17 at 16:02
  • also on some systems an `int` might not be big enough to store a memory address - that's why using the correct data type is important. – Chris Turner Jun 26 '17 at 16:04
  • Thanks all anyway :) – UkFLSUI Jun 26 '17 at 16:07
  • 5
    And C has arrays, the source that you are pointing to is really, really bad. – Jens Gustedt Jun 26 '17 at 16:12
  • 2
    Both of the two program fragments you present have undefined behavior, and in that sense they are not valid C. This is because a pointer is not at all the same thing as the object it points to -- the two don't even have the same type. In each case, therefore, one of the `printf()` calls has an argument whose type does not match the one required by the provided format string. – John Bollinger Jun 26 '17 at 16:29
  • 2
    Additionally, no, you absolutely cannot avoid pointers by using the `&` operator, because ***the result of applying that operator is a pointer***. You can that way reduce your use of *variables* of pointer type, but it does nothing at all for avoiding use of pointers. – John Bollinger Jun 26 '17 at 16:41
  • 2
    Moreover, no, the `&` operator does not help you with dynamic memory management, because to use it you need first to have an object to apply it to. That's exactly what you *don't* have when you're allocating memory dynamically. – John Bollinger Jun 26 '17 at 16:44

1 Answers1

5

Answering questions in reverse order:

C does not have array variables....but this is really just working with pointers with an alternative syntax.

This is incorrect, and you need to toss that bookmark in the trash. It's a common misconception that arrays and pointers are the same thing, but they are not. An array expression will be converted to a pointer expression under most circumstances, and array subscripting is accomplished through pointer arithmetic, but an array object is an actual array, not a pointer.

If pointer also keeps the variable's address in it and at the same time we can get the variable's address using & sign, can't we do the same task pointer does by deriving the address of any variable? I mean if I don't declare it as pointer and use it as int ptr = &i; in 2nd code snippet and use it as normal variable, what would be the differences?

That code doesn't illustrate why pointers exist, or why they are useful.

C actually requires us to use pointers in the following cases:

  1. To write to a function's parameters;
  2. To track dynamically allocated memory;

Pointers also make dynamic data structures like trees and lists easy to implement, but they aren't required for it (unless you're using dynamic memory allocation in those structures).

Writing to a function's parameters

C passes all function arguments by value; the formal parameter in the function definition is a separate object in memory from the actual parameter in the function call, so any change to the formal parameter is not reflected in the actual parameter. For example, assume the following swap function:

void swap( int a, int b ) { int t = a; a = b; b = t; }

This function exchanges the values in a and b. However, when we call the function as

int x = 4, y = 5;
swap( x, y );

the values of x and y won't be updated, because they are different objects than a and b. If we want to update x and y, we have to pass pointers to them:

swap( &x, &y );

and update the function definition as follows:

void swap( int *a, int *b ) { int t = *a; *a = *b; *b = t; }

Instead of swapping the contents of a and b, we swap the contents of the objects that a and b point to. This crops up all the time - think about the scanf function, and how you have to use the & operator on scalar arguments.

Tracking dynamically allocated memory

The dynamic memory allocation functions malloc, calloc, and realloc all return pointers to dynamic memory buffers; there's no variable associated with that memory as such.

char *buffer = malloc( sizeof *buffer * some_length );

A pointer is the only way to track that memory.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Actually the link I was following was the reason I fell into those confusions. :( – UkFLSUI Jun 26 '17 at 18:46
  • 2
    @RakibulIslam: C has been poorly taught for decades, and there are a *lot* of misinformed people out there who pass that misinformation on to others. If you haven't already done so, bookmark the latest [online draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) of the C language standard. It's not a great *teaching* resource (it doesn't have any sort of tutorial information), but it is the authoritative document of how various things work in C, – John Bode Jun 26 '17 at 20:52
  • 1
    You answered this well: I didn't think it was possible. Have an upvote! – Bathsheba Jun 27 '17 at 06:59