Thread: Pointer changing values

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    48

    Pointer changing values

    Below is my code. The problem I am having is that when trying to print out a value in the main function from a pointer in a function its giving me wacky values. Like for example my maximum value lets say should be 90, instead its giving me 16, for minimum should be 19 but its giving me 3098789, and for gtr its giving me 16 again when it should be 1. All my logic should be correct its just the way my pointers are i believe is what is giving me the problem and thats where i am stuck.
    Code:
     Program Purpose: To find the max and min in a function and to find
    				   the average and number greater than average in another
    				   function from an input file.*/ 
    #include<stdio.h>
    #include<math.h>
    double average(int x[], int npts, int *gtr);
    void ranges(int x[], int npts, int *max_ptr, int *min_ptr);
    int main(void)
    {
    	/*Intialize Variables*/
    	int k=0;
    	int npts;
    	int x[20]; 
    	FILE *inp;
    	double ave;
    	int *max_ptr;
    	int *min_ptr;
    	int *gtr;
    	int num;
    
    
    	/*Open File and read data into array.*/
    	inp=fopen("input7.dat","r");
    	if(inp==NULL)
    		printf("Error opening input file. \n");
    	else
    	{
    		fscanf(inp, "%d", &npts);
    			for (k=0; k<npts; k++)
    				fscanf(inp, "%d", &x[k]);
    	}
    	ranges(x, npts, max_ptr, min_ptr);
    	printf("Maximum is %d and minimum is %d \n", max_ptr, min_ptr);
    	ave = average(x, npts, gtr);
    	printf("Average is: %lf \n", ave);
    	printf("Number of values in array that are greater than average: %d \n", gtr);
    	return 0;
    }
    /*-----------------------------------------
    This function determines max and min values from one
    dimensional array.*/
    void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
    {
    	/*Declare Variables*/
    	int k;
    	int max_x;
    	int min_x;
    	
    	/*Determine maximum value in the array*/
    	max_x = x[0];
    	for (k=1; k<=npts-1; k++){
    		if(x[k]>max_x)
    			max_x=x[k];}
    	max_ptr = max_x;
    	
    	
    	
    	/*Determine minimum value in the array*/
    	min_x = x[0];
    	for (k=1; k<=npts-1; k++)
    		if(x[k]<min_x)
    			min_x=x[k];
    	min_ptr=min_x;
    	
    	return;
    }
    /*--------------------------------------------------
    This function returns the average value and determines
    the occuarance of numbers greater than average.*/
    double average(int x[], int npts, int *gtr)
    {
    	/*Declare Variables*/
    	int k;
    	double sum=0;
    	double ave_x;
    
    	/*Compute*/
    	for (k=0; k<=npts-1; k++)
    		sum+=x[k];
    	ave_x = sum/npts;
    	gtr = 0;
    	for (k=0; k<=npts-1; k++)
    		if(x[k] > ave_x)
    			gtr++;
    
    	/*Return mean*/
    	return ave_x;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    ranges(x, npts, max_ptr, min_ptr);
    printf("Maximum is &#37;d and minimum is %d \n", max_ptr, min_ptr);
    Lots of mistakes
    1. max_ptr and min_ptr are pointing nowhere
    2. %d is not the way to print them
    3. Nor are you printing what they point to.

    Try
    int max, min;
    ranges(x, npts, &max, &min);
    printf("Maximum is %d and minimum is %d \n", max, min);
    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.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I belive this line
    Code:
    printf("Maximum is &#37;d and minimum is %d \n", max_ptr, min_ptr);
    should read like this:
    Code:
    printf("Maximum is %d and minimum is %d \n", *max_ptr, *min_ptr);
    And, the value pointed to by gtr should be an address within your x[] array, and the value of gtr should never start at zero.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
    {
    	/*Declare Variables*/
    	int k;
    	int max_x;
    	int min_x;
    	
    	/*Determine maximum value in the array*/
    	max_x = x[0];
    	for (k=1; k<=npts-1; k++){
    		if(x[k]>max_x)
    			max_x=x[k];}
    	max_ptr = max_x;
    
    That doesn't assign your pointed to value - it assigns the pointer itself with max_x - which is an integer. Do you not get at least a warning on that line? [And there is another line like that a few lines further down, dealing with min]

    On another note, if you loop through the array looking for max, why not check for the min value in the same loop? It will be nearly twice as fast, as you don't have all the overhead of the loop itself - just one more if-statement that is unlikely to be true every iteration.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You should dereference what max_ptr and min_ptr are.


    use

    Code:
    *max_ptr = max_x;
    
    ...
    
    *min_ptr=min_x;

    If your intent is to assign values to those pointers....

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Code:
    #include "stdafx.h"
    #include<stdio.h>
    #include<math.h>
    double average(int x[], int npts, int *gtr);
    void ranges(int x[], int npts, int *max_ptr, int *min_ptr);
    int main(void)
    {
    	/*Intialize Variables*/
    	int k=0;
    	int npts;
    	int x[20]; 
    	FILE *inp;
    	double ave;
    	int *max_ptr;
    	int *min_ptr;
    	int *gtr;
    	int num;
    
    
    	/*Open File and read data into array.*/
    	inp=fopen("input7.dat","r");
    	if(inp==NULL)
    		printf("Error opening input file. \n");
    	else
    	{
    		fscanf(inp, "%d", &npts);
    		for (k=0; k<npts; k++)
    			fscanf(inp, "%d", &x[k]);
    	}
    	ranges(x, npts, max_ptr, min_ptr);
    	printf("Maximum is %d and minimum is %d \n", max_ptr, min_ptr);
    	ave = average(x, npts, gtr);
    	printf("Average is: %lf \n", ave);
    	printf("Number of values in array that are greater than average: %d \n", gtr);
    	return 0;
    }
    /*-----------------------------------------
    This function determines max and min values from one
    dimensional array.*/
    void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
    {
    	/*Declare Variables*/
    	int k;
    	int max_x;
    	int min_x;
    
    	/*Determine maximum value in the array*/
    	max_x = x[0];
    	for (k=1; k<=npts-1; k++){
    		if(x[k]>max_x)
    			max_x=x[k];}
    	// ERROR: Cannot convert int to int*
    	//max_ptr = max_x;
    
    
    
    	/*Determine minimum value in the array*/
    	min_x = x[0];
    	for (k=1; k<=npts-1; k++)
    		if(x[k]<min_x)
    			min_x=x[k];
    	// ERROR: Cannot convert int to int*
    	//min_ptr=min_x;
    
    	return;
    }
    /*--------------------------------------------------
    This function returns the average value and determines
    the occuarance of numbers greater than average.*/
    double average(int x[], int npts, int *gtr)
    {
    	/*Declare Variables*/
    	int k;
    	double sum=0;
    	double ave_x;
    
    	/*Compute*/
    	for (k=0; k<=npts-1; k++)
    		sum+=x[k];
    	ave_x = sum/npts;
    	gtr = 0;
    	for (k=0; k<=npts-1; k++)
    		if(x[k] > ave_x)
    			gtr++;
    
    	/*Return mean*/
    	return ave_x;
    }
    See the comments I added!

    And then the usual list of mistakes/problems:
    Warning 1 warning C4101: 'num' : unreferenced local variable g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 17
    Warning 2 warning C4100: 'min_ptr' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 40
    Warning 3 warning C4100: 'max_ptr' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 40
    Warning 4 warning C6273: Non-integer passed as parameter '2' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 31
    Warning 5 warning C6273: Non-integer passed as parameter '3' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 31
    Warning 6 warning C6273: Non-integer passed as parameter '2' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 34
    Warning 7 warning C6001: Using uninitialized memory 'min_ptr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Warning 8 warning C6001: Using uninitialized memory 'max_ptr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Warning 9 warning C6001: Using uninitialized memory 'gtr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30, 31, 32 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 32
    Warning 10 warning C6001: Using uninitialized memory 'npts': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 23, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Warning 11 warning C4700: uninitialized local variable 'min_ptr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Warning 12 warning C4700: uninitialized local variable 'max_ptr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Warning 13 warning C4700: uninitialized local variable 'gtr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 32
    Warning 14 warning C4701: potentially uninitialized local variable 'npts' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
    Complete with line-numbers and all...
    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 slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also your priintf statement should be...

    Code:
     printf ("Maximum is &#37;d and minimum is %d \n", *max_ptr, *min_ptr);
    Note the dereferencing of those pointers!

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i got my max and min to work, but i still need help with my gtr pointer, its still not returning the right value here is what i have.
    function prototype
    Code:
    double average(int x[], int npts, int *gtr);
    calling function
    Code:
    ave = average(x, npts, &gtr);
    function
    Code:
    double average(int x[], int npts, int *gtr)
    {
    	/*Declare Variables*/
    	int k;
    	double sum=0;
    	double ave_x;
    
    	/*Compute*/
    	for (k=0; k<=npts-1; k++)
    		sum+=x[k];
    	ave_x = sum/npts;
    	*gtr =0;
    	for (k=0; k<=npts-1; k++)
    		if(x[k] > ave_x)
    			*gtr++;
    
    	/*Return mean*/
    	return ave_x;
    }
    but now it prints out 0 as my gtr when it should print out 1

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you expect *gtr++ to add one to the value of *gtr? It doesn't.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *gtr++
    is the same as
    *gtr
    gtr++

    If you want to add 1 to *gtr, you need to do (*gtr)++.
    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. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM