Thread: pointers help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    63

    pointers help

    I've been working on this C programming assignment all week and am getting nowhere, its due monday so I'm running out of time. Any help would be greatly appreciated, thank you.

    CIT 145 Pointer Assignment

    Construct a program that declares a size 10 array of doubles and an array of pointers to doubles.

    In a for loop, using pointer arithmetic to increment, initialize the integer array from 1.0 to 10.0 and in same loop initialize the pointer array to point to the array elements holding the data.

    Create a print function that accepts the pointer array and its size as an argument. The function should, using square bracket notation, print out the contents of the pointer array and the values in the double array.

    An possible example of output:

    0x001f20 1.0
    0x001f28 2.0

    etc.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And the code you have so far...?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    Im almost too embarrassed to post it, I think I was closer o few days ago than it is now.

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    int main()
    {
    	int ptr;
    	double values[10];	
        void array(double *ptr[10]);
        int i;
    
       
    
    	for (ptr = 0; ptr < 10; ptr++ ) {
    		values[ptr]=1;
    		
    	}
    	for (i = 1; i< 11; i++) {
    	
    		printf("%p%3d\n", &values[i], i);
    
       
    
    
    	}
    }

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I'm afraid that looking at this code will lower IQ's!

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If this is to have meaning...
    Code:
    #define SIZE 10
    ...then you may need to use it.

    C doesn't have much magic, so...
    Code:
    void array(double *ptr[10]);
    ... is going a little overboard. An array of pointers would be:
    Code:
    double *ptr[10];
    This doesn't change much:
    Code:
    values[ptr]=1;
    does it?

    This looks troublesome:
    Code:
    for (i = 1; i< 11; i++)
    If you can't explain "magic" numbers such as one and eleven, maybe it's not quite correct.

    It may not be as far off as you think, though.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    when I change to

    Code:
    double *ptr[10];
    I get an error message:
    C:\Documents and Settings\Joshua\Desktop\CIT-145\trial.c(9) : error C2040: 'ptr' : 'double *[10]' differs in levels of indirection from 'double '

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jsbeckton
    when I change to

    Code:
    double *ptr[10];
    I get an error message:
    C:\Documents and Settings\Joshua\Desktop\CIT-145\trial.c(9) : error C2040: 'ptr' : 'double *[10]' differs in levels of indirection from 'double '
    Where? Post the associated code or else your note may not have any meaning.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    here it is after change, thank you for your help by the way...

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    int main()
    {
    	double ptr;
    	double values[10];	
        double *ptr[10];
        int i;
    
       
    
    	for (ptr = 0; ptr < 10; ptr++ ) {
    		values[ptr]=i;
    		
    	}
    	for (i = 1; i< 11; i++) {
    	
    		printf("%p%3d\n", &values[i], i);
    
       
    
    
    	}
     
    
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How many variables with the name ptr do you have? And where is the print function?

    [edit]If in pointer notation values[i] is *(values + i)...
    Last edited by Dave_Sinkula; 10-07-2005 at 09:13 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I thought that I needed to declare ptr as a double and then use it as a array of pointers to doubles.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Construct a program that declares a size 10 array of doubles and an array of pointers to doubles.
    It might not be an assignement I'd create, but I'll work with what's there.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    in my first for statement I was trying to initilize the pointer array to point to the array elements holding the data.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    in the second I was trying to print out the contents of the pointer array and the values in the double array

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I was a bit thrown by that too, but i'm lost anyways

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The second loop is supposed to be a function, so I pointed you that way.

    The first loop is supposed to use pointer notation, so I was trying to steer you that way. This is way too much of a clue, but I need another beer and a cigarette:
    Code:
          *(values + i) = i + 1;
          *(ptr    + i) = values + i;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM