I am learning C++ and I have done Pointers..but I am not able to understand it well. I know what is it but I am not able to understand exactly why do we use rather where do we use it. :confused: .basically it's the practical implication of it which I am not able to understand..So can anyone help me in making me understand this POINTER...:)

Basically pointers is vast topic.....so cant explain u here .However cud u point out any specific area where u r getting problem.

In general, I'd say pointers are like links on the internet. They refer you to something, and that's all they do. Pointers don't allocate memory, they just point to memory. Pointers aren't a structure, they refer (point) to a structure.

The problem is that most people's first interaction with pointers is allocating memory on the heap, so they start to associate pointers with the heap. I remember the first time I had to pass a structure to the win32 api. I acutally allocated it on the heap and destroyed it after the call! lol. I know quite a few others who've done similar things, so, IMHO, that's a big pitfall.

The way something like that should be thought of is when you allocate the memory, either by using new, malloc, or any other memory allocation method, they return you a pointer. Now, that pointer they returned isn't the heap memory you just allocated, it only points to it. You could assign that pointer to something else, but that memory is still where it was (and you would have just leaked it, lol). All the pointer does is refer to where it starts.

I dunno, that was my biggest hurdle back in the day. What's your current issue?

i'm also a bit confused. for example, all of the following code works just fine:

#include <stdio.h>

main()
{
  char c[100];
  scanf("%s", &c);
  printf("test %s", &c);
}
#include <stdio.h>

main()
{
  char c[100];
  scanf("%s", &c);
  printf("test %s", c);
}
#include <stdio.h>
main()
{
  char c[100];
  scanf("%s", c);
  printf("test %s", c);
}

please explain which is best and why.

i'm also a bit confused. for example, all of the following code works just fine:

please explain which is best and why.

In all cases, main should be declared as returning an int, and an int value should be returned from it.

#include <stdio.h>
main()
{
  char c[100];
  scanf("%s", &c);
  printf("test %s", &c);
}

The type of the second argument to scanf is incorrect, and the type and value of the second argument to printf are incorrect.

#include <stdio.h>
main()
{
  char c[100];
  scanf("%s", &c);
  printf("test %s", c);
}

The type of the second argument to scanf is incorrect, but the type and value of the second argument to printf are correct.

#include <stdio.h>
main()
{
  char c[100];
  scanf("%s", c);
  printf("test %s", c);
}

The types and values are correct for both.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.