Thread: Pointers to arrays problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Pointers to arrays problem

    Hi Guys. I'm writing a program to accept some values, store them in arrays, then pass these arrays back the to caller to be printed. But when I print, i get weird output. like...0.002.007. Can anyone please help me to identify the problem?

    Code:
    void listValues()
    {
        
        
        int i;
        char carModelArray[MAX] = {0};
        float sLengthArray[MAX] = {0};
        float rSlopeArray[MAX]= {0};
        float speedArray[MAX] = {0};
        
            
        readValues(&carModelArray,&sLengthArray,&rSlopeArray,&speedArray);
        for (i = 0; i < 2; i++)
        {
        printf("%.3f",speedArray[i]);
        }
        getch();
    
    }
        
    //Module that allows the user to enter the values
    void readValues(char (*carModelArray)[MAX],float (*sLengthArray)[MAX],float (*rSlopeArray)[MAX],float (*speedArray)[MAX])
    {
        char carModel[25];
        float speed,sLength = 0,rSlope;
        int i = 0;
        while(sLength != 999)
        {
        
        printf("\nPlease enter the model of the car ");
        scanf("%s", &carModel[i]);
        strcpy((*&carModelArray)[i],&carModel[i]);
        
        printf("\nEnter the skid length made by the car");
        scanf("%f", &sLength);
            
        while(sLengthArray<=0 )
        { 
               printf("\nThe skid length must be greater than zero <0>");
               scanf("%f", &sLength);
        }
        (*sLengthArray)[i] = sLength;
        
        printf("\nEnter the slope of the road ");
        scanf("%f", &rSlope);
        
        while(rSlope<-30|| rSlope >30)
        {
            printf("\nThe slope of the road must be greater than -30 and less than 30");
            printf("\nEnter the slope of the road ");
            scanf("%d", &rSlope); 
            }
        (*rSlopeArray)[i] = rSlope;
        i++;
    
        speed = CalculateSpeed(sLength,rSlope);
        (*speedArray)[i] = speed;
    }
    }
    
    //Module to calculate the speed of the car
    
    float CalculateSpeed(float skd_lgnth, float rd_slp)
    {
            float slopeRad,slopeTangent;
           
           float speed,cosine,sine,answer;      
            
            
            
           slopeRad = rd_slp*(pi/180);
           slopeTangent = (sin(slopeRad))/(cos(slopeRad));
           answer = slopeTangent*(pi/180);
            
            
            
            
            speed= sqrt(30*skd_lgnth*(answer+Brakefactor));
            return speed;
            
            
            getch();   
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    first off idk if this is right
    void readValues(char (*carModelArray)[MAX],float (*sLengthArray)[MAX],float (*rSlopeArray)[MAX],float (*speedArray)[MAX]);

    shouldnt it be void readValues(char *carModelArray, float *sLengthArray, float *rSlopeArray, float *speedArray)?
    because if you use the (*carModelArray) you are derefensing it with out assigning it to another array.
    You could say
    Code:
     readValues( carModelArray, sLengthArray, rSlopeArray, speedArray);
    
    void readValues(char *carModelArray, float *sLengthArray, float *rSlopeArray,float *speedArray)
    {
      char *Array = *carModelArray;
      and at the end just switch it.
      *carModelArray = *Array
    }
    thats the quick fix way but your trying to pass the addresses of the array not the array itself. So if you step through the array you wont know where the end is because its just the address not the actual array.
    Last edited by kille6525; 02-09-2010 at 04:32 AM. Reason: quick note.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's correct. It's a pointer to an array, as opposed to a pointer to the first element.
    However, it is suspect since you use the i variable as the index, and there is only one array, so i = 1 would try to access the second array which does not exist...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Thanks so far. Well I'm not sure wut you mean by "one array". readvalues loops until 999 is entered for sLength . And after every loop all arrays change to their second element. I dont get wut u mean by it does not exist. Do you mean in listvalues? Please explain it to me. Thanks alot.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't figure out why you don't declare our arrays in main(), (or in a function that calls all the others), and then pass them around from there?

    You can have all the addresses you want, but if the array itself is out of scope, that's VERY dodgy indeed.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have an array of MAX elements. You pass a pointer to the array into the function.
    So if the array's name is a, then a[0] means the 1st array, the 0th element of the array of arrays.
    a[1] doesn't exist because you have only one array.

    Basically, you can see it like this:
    Assume that in the computer memory, you have n arrays laid out after each other. Each array is a storage of n elements of n bytes. Total they take up m bytes. So each array takes up m bytes, located after each other (contiguous) in memory.

    Then a[0] would access the array at the memory location where a is pointing, and a[1] would access the array at a + m bytes, and so on.
    But you have only one array of m bytes, hence that would be undefined.
    You can also create a pointer to the first element of an array, which allows you to travel each element in the array. Then a[0] would refer to the first element, and a[1] to the 2nd, and so on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    @ Adak. I have to do it this way.

    @ Elysia. Thanks alot for the explanation. I think i understand what you're saying. However, I'm not sure how to go about making more than one array though. I thought i had the format correct =( Help me out please?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you need more than one array or just fill the individual elements of one array? It seems the later is more likely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    i only need to fill the elements of one array. But I have to fill elements into carModelArray, sLengthArray, rSlopeArray, speedArray. I tried using the pointers to simply edit the values by reference. But it doesnt seem to work out well. I think the problem lies within not being able to move on to the next address properly. Can you think of the solution?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you would change to:
    Code:
    void readValues(char* carModelArray, ...)
    Then carModelArray[0] would be the first element, carModelArray[1] would be the second element, and so on.
    You could also do:
    *carModelArray would get the first element.
    carModelArray++;
    *carModelArray would then get the second element.
    Don't forget to pass along the size of the array and make sure that you don't write beyond the end!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Hi again. I did wut u advised. But now it tells me, "passing arg 1 of readValues from incompatible pointer type" and it says that for arg 2, 3 and 4 as well.

    Code:
    void listValues()
    {
        
        
        int i;
        
        
            
        readValues(&carModelArray,&sLengthArray,&rSlopeArray,&speedArray);
        for (i = 0; i < 2; i++)
        {
        printf("%.f",speedArray[i]);
        }
        getch();
    
    }
        
    //Module that allows the user to enter the values
    void readValues(char *carModelArray,float *sLengthArray,float *rSlopeArray,float *speedArray)
    {
        char carModel[25];
        float speed,sLength = 0,rSlope;
        
        while(sLength != 999)
        {
        
        printf("\nPlease enter the model of the car ");
        scanf("%s", &carModel);
        strcpy(carModelArray,carModel);
        carModelArray++;
        
        printf("\nEnter the skid length made by the car");
        scanf("%f", &sLength);
            
        while(sLengthArray<=0 )
        { 
               printf("\nThe skid length must be greater than zero <0>");
               scanf("%f", &sLength);
        }
        
        *sLengthArray = sLength;
        sLengthArray++;
        
        printf("\nEnter the slope of the road ");
        scanf("%f", &rSlope);
        
        while(rSlope<-30|| rSlope >30)
        {
            printf("\nThe slope of the road must be greater than -30 and less than 30");
            printf("\nEnter the slope of the road ");
            scanf("%d", &rSlope); 
            }
        *rSlopeArray = rSlope;
        rSlopeArray++;      
                                
        getch();
        
        system("PAUSE");
        
        speed = CalculateSpeed(sLength,rSlope);
        *speedArray = speed;
        speedArray++;
        // printf("%.3f",(*speedArray)[0]);
        
    }
    }

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Assuming carModelArray is an array, remove the &,
    So &carModelArray becomes carModelArray.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Need Help with pointers and arrays
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2005, 04:46 PM
  4. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM