+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    dnuof-dna-tsol lost-and-found's Avatar
    Join Date
    Oct 2001
    Location
    Left Coast...aka CA
    Posts
    3,103

    C and pointers help

     
    Wow, it's been a while since I actually asked for help here, but here it goes.

    I'm writing a C program that is using some pointers, pointers to pointers, etc.

    Here's a small example of what my code needs to do
    Code:
    typedef struct{
      int num_tasks;
      int* task_list;   //a variable sized array of tasks 
    } entry;
    typedef struct{
      entry *ref_to_entry;
    } entry_node_t;
    int main(){
      /* some code here that is irrelevant */
      entry *user_entry;
      user_entry = (entry*) malloc (num_entries*sizeof(entry)); //assume num_entries is obtained from user input during runtime
      
      entry_node_t* entry_node;
      entry_node = (entry_node_t*) malloc(num_entries * sizeof(entry_node_t));
      for(int i=0;i<num_entries;i++)
       entry_node = &user_entry[i];
    }
    Basically, in the above code I would like entry_node to be an array of pointers to each user_entry. I know I could just store the index to entry inside entry_node, but I would like it to be a pointer so that I don't have to write:
    Code:
    entry[entry_node[j]].num_tasks;
    but instead use
    Code:
    entry_node[j]->num_tasks;
    Am I doing something wrong? I try to compile and it compiles, but it doesn't seem to allocate space for entry_node.

  2. #2
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,330
    I forget the exact syntax, but can you try to "catch" the exception if malloc fails?

    That one program way back when messed with my head too...an array of pointers to pointers...ugh! ROFL

    Good Luck...if you need more help I will, but this stuff gives me headaches!!

    I thought we coded something like: int **data_ptr;
    Last edited by Rootstonian; March 15th, 2009 at 10:32 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. c++ pointers
    By lost-and-found in forum Webmastering and Programming
    Replies: 5
    Last Post: August 23rd, 2004, 08:21 PM
  2. Pointers?
    By Brainchild in forum IMO Community
    Replies: 8
    Last Post: July 23rd, 2004, 06:58 PM
  3. C++ - Pointers and Practicality
    By Iturea in forum Webmastering and Programming
    Replies: 3
    Last Post: January 22nd, 2004, 11:39 PM
  4. Pointers as parameters...HELP!
    By squeech in forum Webmastering and Programming
    Replies: 1
    Last Post: September 16th, 2003, 10:45 PM
  5. C++ pointers
    By Damien019 in forum Webmastering and Programming
    Replies: 5
    Last Post: May 1st, 2003, 11:00 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews