LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2003, 11:32 PM   #1
KendersPlace
Member
 
Registered: Feb 2003
Location: Phoenix, AZ - USA
Distribution: RedHat 8, Micro$haft
Posts: 33

Rep: Reputation: 15
Lightbulb C: What's the point of 'pointers'??


I've been going through a C text book over the past week, and am now on the first chapter dealing with "pointers". The book states again and again that pointers are a key fundamental concept that is at the heart of all C programming. It goes on and on about how important the concept of Pointers is, but never actually tells me WHY. ??

I've written code in PHP, ASP, and done lots of database development over the years, but this is the first time I've actually attempted to learn a "real" true programming language.

So far, I see that a pointer is a way to basically access a memory block by alias. But why? If I have memory set aside for the variable "my_value" why would I ever want to call it by some alias? Why not just use the original name? This pointer business seems like it's just an extra and unecessary step in the process.

Could someone explain why this topic is so important, and how one would actually put the concept to real use in an actual real-world program?

Thanks.
 
Old 09-04-2003, 12:08 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
There are many, many uses for pointers. Here are a couple, and this is by no means comprehensive.

If you want to have a function change the value of an argument you give it, you need to use a pointer. When called, a function receives a copy of a variable's value. The way around this is to give the function a pointer to the variable. Essentially, you're saying, here's a copy of where the variable is in memory. Then, inside the function, you tell the program to write *to* that location in memory.

Another classic example is this: what if, when writing your program, you don't know how much memory you need to hold your data? For instance, the program might need to read in a list of customers in a database that changes hourly. If you don't use pointers, you would have to declare an array/structure to be the absolute maximum size you think it could ever be. That's not very efficient... What if other programs are trying to run, and you had an array consuming 100 MB of memory, but only needed 2 KB? On the flip side, what happens if your assumption is wrong, and you actually need more memory than you thought was the worst case? With a pointer, you can have your program wait until it knows how much memory it needs (by asking the user, calculating it based on arguments, the size of an input file, etc), and then request the memory. The system comes back giving you a pointer to the block of memory it reserved for you. Then you can use that pointer as a reference point to fiddle with the contents of that memory. That way, you only use the exact amount of memory your program needs.
 
Old 09-04-2003, 02:18 AM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Pointers are for programming languages that don't give you nice namespaces and scoping and garbage collection. The "problems" that Dark_Helmet stated, while genuine in their own respects, aren't really true problems in a more general sense.

Having a function change the value of an argument you give it
This isn't really a problem in the sense that you never HAVE to do this. Anything you can do in any given programming language can be done in a purely functional language, which never modifies the values of variables.

Being stingy with memory
With dynamically typed languages, you can simply declare the unused portions of a data structure to be some null value which requires very little storage.


Pointers are important if you are going to learn C, absolutely. But they are a BIG pain in the ass. In fact, I'd say that misuse of pointers accounts for a large portion of the problems with applications today - buffer overflows, segmentation faults, etc. So, if you can ever just NOT deal with pointers, you'll be better off.
 
Old 09-04-2003, 04:47 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
Quote:
The "problems" that Dark_Helmet stated, while genuine in their own respects, aren't really true problems in a more general sense.
perhaps true, but tell that to the poor sod who's been assigned to write a high level language compiler for a fancy new processor thats come out which only has a very minimilistic C compiler.

KendersPlace: C is sometimes known as high level assembly, thats because it essentially is. pointers are a very fundamental concept in assembly and so have been transferred to C, every language has some sort of pointers but they try to hide the implementation, eg every variable that you 'new' in java is a pointer, the language just doesnt let you see it.

have you met arrays/strings yet? these are pointers. are you familiar with complex data structures like treess, linked lists, etc. try to implement a few data structures and you'll find pointers will come to your rescue. i probably have a few implementations of various data structures i wrote ages ago, i'll post them if you want to see/disect them.
 
Old 09-04-2003, 05:44 PM   #5
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
please do post them...

just so i can see/understand exactly what you're talking about ;)
 
Old 09-04-2003, 07:35 PM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ive found what i think is my first linked list but i cant find any other C data structures ive written, must be on a different hard disk, i'll have a look tomorrow. anyway heres my first linked list

Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct LL_NODE {
  struct LL_NODE *next, *prev;
  int data;
} ll_node;


//inserts node containig data before/after node *where. if where is NULL then creates a
//standalone node, returns address of new node
ll_node *ll_insert(int data, ll_node *where, int after)
{
  ll_node *temp = NULL;

  if((temp = (ll_node *)malloc(sizeof(ll_node))) == NULL) {
    fprintf(stderr, "malloc failed!!\n");
    return NULL;
  }
  temp->data = data;

  if(where == NULL) {
    temp->prev = NULL;
    temp->next = NULL;
    return temp;
  }

  if(after) {
    temp->next = where->next;
    temp->prev = where;
    where->next = temp;
    if(temp->next != NULL) {
      temp->next->prev = temp;
    }
  } else {
    temp->next = where;
    temp->prev = where->prev;
    where->prev = temp;
    if(temp->prev != NULL) {
      temp->prev->next = temp;
    }
  }

  return temp;
}

//pretty obvious
void ll_remove(ll_node *node)
{
  if(node != NULL) {
    if(node->prev != NULL) node->prev->next = node->next;
    if(node->next != NULL) node->next->prev = node->prev;
    free(node);
  }
}

//starting at *start goes in dir and looks for data, returns node containg data
ll_node *ll_find(int data, ll_node *start, int dir)
{
  ll_node *curNode = start;

  while(curNode != NULL) {
    if(curNode->data == data) break;

    if(dir)
      curNode = curNode->next;
    else
      curNode = curNode->prev;
  }

  return curNode;
}

void print(ll_node *start)
{
  ll_node *curNode = start;

  while(curNode != NULL) {
    printf("[%d]", curNode->data);
    curNode = curNode->next;
  }
  printf("\n");
}

int main()
{
  int i;
  ll_node *top = NULL, *cur = NULL;
  
  //put '1' through '10' in list
  top = ll_insert(1, NULL, 0);
  cur = top;
  for(i=2;i<11;i++) 
    cur = ll_insert(i, cur, 1);
  print(top);

  //remove 5
  ll_remove(ll_find(5, top, 1));
  print(top);

  //add it
  ll_insert(5, ll_find(6, top, 1), 0);
  print(top);

  //really should clear up but this is data structure test
  //not a good program, let windows clear up for me.
  return 0;
}
 
Old 09-05-2003, 05:30 PM   #7
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
pretty cool, thanks

i've heard how FUN linked lists can be ...especially doublies ;)

i've actually been reading about using a doubly linked list to dynamically allocate memory for an isometric game engine i would like to create, (much better than using fixed arrays, but a little more difficult to implement effectively (for someone as newbie as i)), where as there may be several layers of tiles in memory (but not necessarily are all layers used on a given coord)

...now if i could just choose to go with C or start learning this OOP stuff ??

Last edited by jhorvath; 09-05-2003 at 05:46 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is WAN point-to-point link? ivj Linux - Networking 1 07-16-2005 01:20 AM
Point to point connectivity between 2 pcs shroffsp Linux - Networking 1 09-22-2003 08:57 AM
Mandrake MNF with point-to-point T1, routers ioannes Linux - Networking 0 07-24-2003 08:59 AM
Is there a detailed point by point comparison on Linux to Windows? Paul Parr Linux - General 4 04-26-2003 02:35 AM
point to point address assignment of ppp0 andyn Linux - Networking 0 10-11-2002 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration