User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2004
Location: India
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Solution Help me understand Pointers

  #1  
Nov 27th, 2004
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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: India
Posts: 57
Reputation: harshchandra is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: Help me understand Pointers

  #2  
Nov 27th, 2004
Basically pointers is vast topic.....so cant explain u here .However cud u point out any specific area where u r getting problem.
Reply With Quote  
Join Date: Aug 2004
Location: Bozeman MT
Posts: 13
Reputation: glSuccinct is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
glSuccinct's Avatar
glSuccinct glSuccinct is offline Offline
Newbie Poster

Re: Help me understand Pointers

  #3  
Nov 29th, 2004
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?
-don't listen to me-
Reply With Quote  
Join Date: Dec 2004
Location: galveston, texas
Posts: 35
Reputation: serfurj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
serfurj's Avatar
serfurj serfurj is offline Offline
Light Poster

Re: Help me understand Pointers

  #4  
Dec 7th, 2004
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help me understand Pointers

  #5  
Dec 7th, 2004
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.
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:02 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC