What's the Point of Pointers?

What's the Point of Pointers?

Post by orwell84 on Sun Mar 15, 2009 3:05 pm
([msg=20014]see What's the Point of Pointers?[/msg])

Okay, so I was reading the tutorial on cprogramming.com about pointers, and I know what they are, but I'm a bit shaky on how they're going to help. How are pointers useful? I know how they "point" to another variable, but I'm just wondering when that will come in handy. I also read that they're most used with Arrays, Structures, and Functions. Why? Thanks in advance for anyone who can clear this up for me.

P.S. Also, I learn something well when I use it, so if you could suggest a program to write that would use pointers, that would be nice. I don't mean "give me a program"; just say what the program should do, and maybe how pointers would be used.
Mens et manus.
User avatar
orwell84
Poster
Poster
 
Posts: 112
Joined: Fri Feb 20, 2009 8:20 pm
Blog: View Blog (0)


Re: What's the Point of Pointers?

Post by BhaaL on Sun Mar 15, 2009 3:15 pm
([msg=20015]see Re: What's the Point of Pointers?[/msg])

They "point" to another variable.
Hm, thats a way to put it. But it actually does not.
A Pointer "points" to the Memory Location of another variable. One could think of them as sort-of aliases to variables.
Code: Select all
int a = 42;
int *b = a;
printf("%d == %d\n", a, *b); //42 == 42
a = 23;
printf("%d == %d\n", a, *b); //23 == 23
*b = 5;
printf("%d == %d\n", a, *b); // 5 == 5


You will need them when you start allocating memory by yourself, rather than letting stuff get allocated on the stack with static sizes.
They are most useful for arrays, considering C knows no strings - they are arrays of single characters. Laid out sequential in Memory, delimited by '\0', you only need to know where the string begins - constant size of a pointer.
Code: Select all
char hello[] = "Hello";
char *world = (char*)malloc(6);
char *ello;
strcpy(world, "World");
printf("%s %s\n", hello, world); //"Hello World"
ello = hello;
printf("%s %s\n", ello, world); //"Hello World"
ello++;
printf("%s %s\n", ello, world); //"ello World"
for (ello = hello; ello != '\0'; ello++)
  printf("%c\n", *ello); //"H", "e", "l", "l", "o"
free(world);
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: What's the Point of Pointers?

Post by orwell84 on Sun Mar 15, 2009 3:30 pm
([msg=20017]see Re: What's the Point of Pointers?[/msg])

Okay, so when is the manual allocation of memory a good thing? I know that lower level languages like C are more efficient than Perl, Python, or Shellcode when it comes to large programs, but what would happen if you didn't allocate it yourself? And when does allocation become necessary? (or, at least, a good idea?)
Mens et manus.
User avatar
orwell84
Poster
Poster
 
Posts: 112
Joined: Fri Feb 20, 2009 8:20 pm
Blog: View Blog (0)


Re: What's the Point of Pointers?

Post by mischief on Sun Mar 15, 2009 10:50 pm
([msg=20033]see Re: What's the Point of Pointers?[/msg])

orwell84 wrote:Okay, so when is the manual allocation of memory a good thing? I know that lower level languages like C are more efficient than Perl, Python, or Shellcode when it comes to large programs, but what would happen if you didn't allocate it yourself? And when does allocation become necessary? (or, at least, a good idea?)


if your program needs to load things dynamically, say a game that loads textures, your program will need to allocate different blocks of memory to store different sized textures. the number of textures will also vary. since you do not know these numbers at runtime, you design the program so that it dynamically allocates memory. if you did try to include these numbers at runtime, your code would either be very memory inefficient, or not work correct. furthermore, it would be bad programming practice altogether.

another example might be a programming language interpreter. when you write the interpreter, you have no idea how much memory your users will want, so you must design it so that memory is allocated when asked for in the interpreted language.
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron