Initialising pointers using a loop.

This is a discussion on Initialising pointers using a loop. within the C Programming forums, part of the General Programming Boards category; Hi, I trying to write a section of code that will initiate a pointer for each iteration of a loop. ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    Initialising pointers using a loop.

    Hi,

    I trying to write a section of code that will initiate a pointer for each iteration of a loop. I have no idea how to do it but this is what i tried.

    Code:
    i++;
              
    struct Employee *ptr"i";
    ptr"i"=test->next;
    but it is not working, anyone know how to do it.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    Code:
    int i;
    for(i=0;i<MAX_COUNT;i++)
    {
       struct Employee* ptr = test->next;
       /* use the ptr here for the current iteration of the loop*/
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Ahh sorry i should have been clearer in my original post, I need to be able to create a desired amount of pointers for use outside the loop.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by will of fortune View Post
    Ahh sorry i should have been clearer in my original post, I need to be able to create a desired amount of pointers for use outside the loop.
    So what you actually need is an array of pointers to structs, from what I gather.

    Try this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct blah{
      int x;
      char y;
    };
    
    int main(int argc, char *argv[]){
      int num_ptrs = 10;
      struct blah **X;
      X = malloc(num_ptrs * sizeof(struct blah*));
      /* X now points to X[0] which is the start of an array of struct blah pointers containing 10 pointers */
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Aha that should work, although i found a way around it so i didn't need to have x amount of pointer, but that may be useful if i ever need it in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21