pointer warning

This is a discussion on pointer warning within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define size 5 int GetSum(int *,int);//can't just prototype as array will only be array[0] need MAX_SIZE int ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    pointer warning

    Code:
    #include <stdio.h>
     #define size 5
     
     int GetSum(int *,int);//can't just prototype as array will only be array[0] need MAX_SIZE
     
     int main(void){
     
     	const int list[size]={23,34,45,56,90};
     	int ndx, sum = 0;
     	int total = 0;
     	int value;
     	
     	for(ndx =0; ndx<size; ndx++){
     		printf("%d = %d\n", ndx, list[ndx]);
     		
     	}
     	value = GetSum(list, size);
     	printf("Value of List using the function is %d\n", value);
     	
     	//sum up elements of array list
     	for(ndx = 0; ndx < 5; ndx++){
     		printf("Sum will be %d + %d\n", sum, list[ndx]);
     		sum = sum + list[ndx];//value at list ndx
     		//sum +=list[ndx]; sum
     
     		printf("Sum is now %d\n", sum);
     }	
     	
     		getchar();
     		return 0;
     }  
     
     GetSum(int list[], int n){
     	int ndx, sum=0;
     	for (ndx = 0; ndx < n; ndx++)
     	   sum +=list[ndx];
     
        return sum;
     }
    i'm getting a warning when this compiles saying
    passing arg 1 of `GetSum' discards qualifiers from pointer target type
    Any ideas why?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The problem is that you're trying to subvert the type system by removing const.

    >int GetSum(int *,int);
    >const int list[size]
    >GetSum(list, size);
    Notice how list is const data, yet the pointer that GetSum takes is not a pointer to const data. Functions that take pointers to non-const data typically change that data, which would be a very Bad Thing in this case because list is indeed const. However, your function doesn't change the list passed to it, so you can change the function like so:
    Code:
    #include <stdio.h>
    
    #define size 5
    
    int GetSum(const int list[],int n);
    
    int main(void){
      const int list[size]={23,34,45,56,90};
      int ndx, sum = 0;
      int value;
    
      for(ndx =0; ndx<size; ndx++){
        printf("%d = %d\n", ndx, list[ndx]);
      }
      value = GetSum(list, size);
      printf("Value of List using the function is %d\n", value);
    
      //sum up elements of array list
      for(ndx = 0; ndx < 5; ndx++){
        printf("Sum will be %d + %d\n", sum, list[ndx]);
        sum = sum + list[ndx];//value at list ndx
        //sum +=list[ndx]; sum
        printf("Sum is now %d\n", sum);
      }	
    
      getchar();
      return 0;
    }  
    
    int GetSum(const int list[],int n){
      int ndx, sum=0;
      for (ndx = 0; ndx < n; ndx++)
        sum +=list[ndx];
    
      return sum;
    }
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    thanks prelude, basically what ever your parameters are the arguments should match up right?

    do you see anything else that might be wrong with the code. I'm still learning pointers and there relationship with arrays. I'd appreaciate the look.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >do you see anything else that might be wrong with the code.
    It looks fine except for the macro. Typically, macros are given names in all capital letters to distinguish them from variables and functions and help them to stand out a bit. And be careful about overflowing sum. In this program it probably doesn't matter, but for anything serious it's something to keep in mind:
    Code:
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    ...
    
    int GetSum(const int list[], int n){
      int ndx, sum = 0;
    
      for (ndx = 0; ndx < n; ndx++) {
        if (sum > INT_MAX - list[ndx]) {
          fprintf(stderr, "Integer overflow detected, aborting...\n");
          exit(EXIT_FAILURE);
        }
        sum += list[ndx];
      }
    
      return sum;
    }
    My best code is written with the delete key.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    how would you overflow sum? wouldn't the loop stop before 5
    Code:
    for(ndx = 0;ndx < 5; ndx++)
    I'm aware of what overflows are, but how they are done I'm not sure I'm entirely clear on that. Wouldn't an individual have to get s/he's hand on the sourcecode and manipulate it?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how would you overflow sum?
    As I said, in this program you wouldn't. But GetSum doesn't know how many values are in list, or how big they are. The potential of overflowing sum if list is a long string of large values is very real. For example, if you (quite reasonably) modified the program to read a file's worth of values into list before passing it to GetSum:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_VALUES 256
    
    int GetSum(const int list[], int n);
    
    int
    main(
      int   argc,
      char *argv[]
      )
    {
      int   list[MAX_VALUES];
      int   total;
      int   val;
      int   n;
      FILE *in;
    
      if (argc != 2) {
        fprintf(stderr, "usage: $prog <filename>\n");
        return EXIT_FAILURE;
      }
      in = fopen(argv[1], "r");
      if (!in) {
        perror(NULL);
        return EXIT_FAILURE;
      }
      for (n = 0; n < MAX_VALUES; n++) {
        if (fscanf(in, "%d", &val) != 1) {
          break;
        }
        list[n] = val;
      }
      if (!feof(in)) {
        fprintf(stderr, "Invalid file format\n");
        return EXIT_FAILURE;
      }
      total = GetSum(list, n);
      printf("The sum of all values is %d\n", total);
    
      return EXIT_SUCCESS;
    }
    Then you have a potential bug on your hands if you don't consider integer overflow within GetSum. A long integer would better serve you for the type of sum, but you would still need to be vigilant. Even as few as 256 numbers can easily overflow even a long.
    My best code is written with the delete key.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Then you have a potential bug on your hands if you don't consider integer overflow within GetSum. A long integer would better serve you for the type of sum, but you would still need to be vigilant. Even as few as 256 numbers can easily overflow even a long.
    I was under the impression that C converts the type for you up to a certain integer. so if int was too small, it would go up to long, then long long. I think the end is max long long. I can't remember.


    [edit]never mind [/edit]
    Last edited by caroundw5h; 07-03-2004 at 11:41 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was under the impression that C converts the type for you up to a certain integer.
    Only in very specific situations. This isn't one of them. It's usually better to assume that if you say you want int, you'll get int, and protect yourself against the problems that it might cause.

    >I think the end is max long long.
    For C89/90 the largest integral type is long. C99 added long long.
    My best code is written with the delete key.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    thats what I thougt. As you can see my book is teaching me c99

  10. #10
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by caroundw5h
    Code:
     int GetSum(int *,int);//can't just prototype as array will only be array[0] need MAX_SIZE
    I come back on that point.
    Well, what must be understood here is that there is no way to directly pass an array to a function. Imagine it could be possible, it would be terrible from the performance point of view with big arrays. The whole data would be copied each time i automatic memory, probably for nothing, because a single pointer to the array is enough to access the whole data block.

    This is why the common interface to deal with arrays is a pointer of the same type.
    Code:
    int GetSum (int *);
    if the function needs to modify the data, or
    Code:
    int GetSum (int const *);
    if the function doesn't modifiy the data.

    But, as you have noticed, there is a missing information here: the numer of elements of the array.

    Despite the appearence, the alternate ways
    Code:
    int GetSum (int[]);
    or
    Code:
    int GetSum (int[123]);
    give no more information, hence the extra parameter. This way is not bad. It's actually the only way to write versatile code.

    However, there are other choices but I'm not sure that it makes the code easier to write, read and maintain.
    • The wrapping structure
      Code:
      typedef struct
      {
         int array[123];
      }
      arr123;
      
      int GetSum (arr123 *);
    • The pointer to array
      Code:
      int GetSum (int (*)[123]);

    but I won't recommend them due to their lack of genercity. However, they can fit with universally fixed size arrays like the number of months or days of a week, things like that...
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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