Thread: Getting the value of a variable rather than pointing to it. Need Help.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    26

    Getting the value of a variable rather than pointing to it. Need Help.

    Hey Hey,

    Code:
    int i;
    int number[100];
    int placeHolder = 0;
    for(i = 0; i < 10; i++)
    {
        number[placeHolder] = i;
        placeHolder++;
    }
    I am writing a local function that is similar to the one above where i want to place the value of i into an array. So that in this example the array would hold the numbers from 0 through to 9.

    Is there any way that i can get the array to accept the value of i and store it without it changing? The program i have writes i in correctly for the 1st loop and then it all goes to hell the second time around.

    Thanks,
    -Nick

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Hmm..... I'm missing it.

    Code:
    include <stdio.h>
    
    int main(void) {
    
        int i , j;
        int number[10];
        int placeHolder = 0;
    
        for(i = 0; i < 10; i++)
        {
            number[placeHolder] = i;
            placeHolder++;
    
        }
    
        for(j=0; j < 10; j++) {
            printf("El numero es: %d\n", number[j]);
        }
    
        return 0;
    }
    
    $gcc -Wall ar.c -o ar
    $./ar
    El numero es: 0
    El numero es: 1
    El numero es: 2
    El numero es: 3
    El numero es: 4
    El numero es: 5
    El numero es: 6
    El numero es: 7
    El numero es: 8
    El numero es: 9
    $

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Hi even i did not find any problem with the code..as it run successfully and printed 0 to 9....if can print the output we can have a try

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by enggabhinandan
    Hi even i did not find any problem with the code..as it run successfully and printed 0 to 9....if can print the output we can have a try
    You're compiler is also ANSI compliant? Maybe your compiler and my compiler should get together and go bowling sometime.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    yes u could be right..............which is the version of the compiler u use and on which platform

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    $uname -a
    Linux linux 2.6.5-7.155.29-default #1 Thu Jun 2 12:07:05 UTC 2005 i686 athlon i3 86 GNU/Linux
    $gcc --version
    gcc (GCC) 3.3.3 (SuSE Linux)
    Copyright (C) 2003 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Thanks for your reply.

    Ok, that was a poorly thought out example. Maybe that isn't the problem.

    I tried to make an example because it'd take ages to explain what the entire purpose of this code is. I have cut out the relevant parts of the code and placed them below. The array road_type holds 0's 1's and 2's in a random order, it's total size is num_roads.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INTO 0
    #define OUTOF 1
    #define TWOWAY 2
    
    /* Prototypes of local functions */
    int get_route_list(int num_roads, int num_routes, int *road_type, int y, int x);
    
    int main(int argc, char *argv[])
    {
       
          /* Create an integer array showing possible routes i.e. road 1 to road 4 would be 14 */
       int j;
       int route_list[num_routes][1];
       for(j = 0; j < num_routes; j++)
       {
          route_list[j][0] = get_route_list(num_roads, num_routes, road_type, j, 0);
          route_list[j][1] = get_route_list(num_roads, num_routes, road_type, j, 1);
       }
       /* -----------------end route list thing-------------------*/
       return 0;
    }
    
         
    
    /* Create an array that lists the possible routes */
    int get_route_list(int num_roads, int num_routes, int *road_type, int y, int x)
    {
       int i, j;
       int route_list[num_routes][1];
       int place_holder = 0;
       for(i = 0; i <= num_roads; i++)
       {
          if(road_type[i] == TWOWAY || road_type[i] == INTO)
          {
             for(j = 0; j <= num_roads; j++)
             {
                if(road_type[j] == TWOWAY || road_type[j] == OUTOF)
                {
                   if(i == j)
                   {}
                    
                   else
                   {
                      route_list[place_holder][0] = i;
                      route_list[place_holder][1] = j;
                      /* #_#_#_#_#_#_#_#_# */
                      place_holder++;
                   }
                }
             }
          }
       }
       return route_list[y][x];
    }
    At that point where the #_#_ bit is the route list array is working fine for the first loop of that j for loop. Then when it loops through again i get problems, the program seems to assign the value of i to the route_list[place_holder][1] = j; I cant work out why this is.

    I'm struggling to explain this without posting the whole code and assignment details. If there is an obvious problem then it'd be great to hear what it is. Otherwise i'll just bang my head against the wall for another couple of hours and hope an answer falls out

    -Nick

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    I use the GNOME Terminal 2.7.3 in a Linux system and compile using the command line gcc play.c -Wall -o play

    thanks to everyone for their input so far

    -Nick

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int route_list[num_routes][1];
    Wrong - you need int route_list[num_routes][2]; if you want to store [0] and [1] elements
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Wicked Awesome

    Thanks, i had changed those for because it got rid of a segmentation fault a while back but forgot to right things again.

    Cheers Salem,
    -Nick

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i = 0; i <= num_roads; i++)
    This is suspicious as well, since the normal loop for accessing an array is < n, not <= n
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM