•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,668 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1958 | Replies: 4
![]() |
•
•
Join Date: Oct 2004
Location: India
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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.
.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 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...
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?
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?
-don't listen to me-
•
•
Join Date: Dec 2004
Location: galveston, texas
Posts: 35
Reputation:
Rep Power: 4
Solved Threads: 1
i'm also a bit confused. for example, all of the following code works just fine:
please explain which is best and why.
#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.
•
•
•
•
Originally Posted by serfurj
i'm also a bit confused. for example, all of the following code works just fine:
please explain which is best and why.
#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);
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Pointers (archived tutorial) (C++)
- Variable pointers or References for .conf parser? (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Pointers (C++)
- Sorting arrays of pointers with function? (C)
- Pointers (Part II) (C)
- Java Expert (Needed) (Java)
Other Threads in the C++ Forum
- Previous Thread: Read and writing strings from structures
- Next Thread: floating point



Linear Mode