why is this pointer showing a large value?

This is a discussion on why is this pointer showing a large value? within the C Programming forums, part of the General Programming Boards category; Code: # include <stdio.h> int main() { int* res[10]; double* vol[10]; double* curr[10]; int i; for (i = 0; i ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    why is this pointer showing a large value?

    Code:
    # include <stdio.h>
    
    int main()
    {
        int* res[10];
        double* vol[10];
        double* curr[10];
        int i;
    
        for (i = 0; i < 5; i++)
        {
            printf("Enter R and V for resistor %d: ", i+1);
            scanf("%d %lf", res+i, vol+i);
        }
        rv(res, vol);
        system("pause");
    }
    
    int rv (int* res, int* vol)
    {
        int i;
        for (i=0; i<5; i++)
        {
            printf("%d curr: %d %lf\n",i+1, *(res+i) , *(vol+i) );
        }
        return;
    }

    This code takes 2 arrays as input (int, double)...
    when I compile the output...

    the *(vol+i) is showing unexpected values. However, if I replace %lf with %d, its working fine.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    When you mean int, print it out as such instead of as a double.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    You are failing to allocate space for or initialize the pointers!

    Get rid of the pointers or take the time to learn how to use them correctly.
    Pointers in C - Tutorial - Cprogramming.com

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Your function int rv (int* res, int* vol) should have 'double *' for vol in the header.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Compile with warnings turned all the way up:
    Code:
    $ gcc -Wall -g -std=c99  arr.c   -o arr
    arr.c: In function ‘main’:
    arr.c:13: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int **’
    arr.c:13: warning: format ‘%lf’ expects type ‘double *’, but argument 3 has type ‘double **’
    arr.c:15: warning: implicit declaration of function ‘rv’
    arr.c:16: warning: implicit declaration of function ‘system’
    arr.c:7: warning: unused variable ‘curr’
    arr.c: In function ‘rv’:
    arr.c:24: warning: format ‘%lf’ expects type ‘double’, but argument 4 has type ‘int’
    arr.c:26: warning: ‘return’ with no value, in function returning non-void

    • Line 13: I think this is what Tim was referring to, but just in case. res, vol and curr are arrays of pointers (to ints or doubles), which are quite different from arrays of ints or arrays of doubles. The pointers in those arrays don't point to valid memory (places you can store data from your scanf call). Remove the * from those declarations.
    • Line 15: either put a prototype for rv above main, or move the whole function above main.
    • Line 16: include stdlib.h to use system
    • Line 7: remove curr if you're not using it
    • Line 24: This was addressed already, but vol should be declared double * in your rv function
    • Line 26: You said rv would return an int, so you must return an int, even if you don't use it in main

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    got it. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 03:18 AM
  2. showing time
    By Micko in forum C Programming
    Replies: 1
    Last Post: 01-02-2004, 06:21 AM
  3. Pic not showing up
    By Hunter2 in forum Game Programming
    Replies: 14
    Last Post: 11-13-2002, 12:19 PM
  4. Showing the *.* files
    By GaPe in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 03:55 AM
  5. Showing two Dialogs?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-07-2002, 12:38 PM

Tags for this Thread


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