Like Tree2Likes
  • 1 Post By AndrewHunter
  • 1 Post By iMalc

How would I fix this incompatible pointer type warning

This is a discussion on How would I fix this incompatible pointer type warning within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to pass in argv into a function that checks the arguments and assigns values for the arguments ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    How would I fix this incompatible pointer type warning

    Hi I'm trying to pass in argv into a function that checks the arguments and assigns values for the arguments passed it. When I try to pass in argv, i get the following warning

    a2.c:80: warning: passing arg 1 of `checkargs' from incompatible pointer type

    what does this warning mean?

    Here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int checkargs(const char * argv[], int *offset, int *length, int *option, const int argc);
    
    int main(int argc, char * argv[])
    {
    	int *option, *length, *offset;
    	size_t i;
    	
    	if(argc < 2 || argc > 4)
    	{
    		printf("USAGE: dump [-bcC] [-nlength] [-soffset] [file]");
    		return 0;
    	}
    
    	checkargs(argv, offset, length, option, argc);
    	printf("Option: %d \nLength: %d\nOffset: %d", option, length, offset);
    }
    
    int checkargs(const char* argv[], int *offset, int *length, int *option, const int argc)
    {
    	size_t i;
    	for(i = 0; i < argc; i++)
    	{
    		if(argv[i][0] == '-')
    		{
    			if(argv[i][1] == 'b')
    			{
    				*option = 1;
    			}
    			
    			if(argv[i][1] == 'c')
    			{
    				*option = 2;
    			}
    			
    			if(argv[i][1] == 'C')
    			{
    				*option = 3;
    			}
    		}
    		
    		if(argv[i][0] == '-')
    		{
    			if(argv[i][1] == 'n')
    			{
    				if(sscanf(argv[i],"-n %d ", &length) != 1)
    				{
    					printf("error getting the length");
    					exit(1);
    				}
    			}
    		}
    
    		if(argv[i][0] == '-')
    		{
    			if(argv[i][1] == 's')
    			{
    				if(sscanf(argv[i],"-s %d ", &offset) != 1)
    				{
    					printf("error getting offset");
    					exit(2);
    				}
    			}
    		}
    	}
    	
    	return 0;
    }
    Thanks for any help in advance

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Look at the difference in your declarations for argv:
    Code:
    int checkargs(const char * argv[], int *offset, int *length, int *option, const int argc);
    
    int main(int argc, char * argv[])
    Do you see the problem?
    stahta01 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I wouldn't be surprised how many people would not see that as a problem since generally one can pass something non-const into something that takes a const. It simply means that the function promises not to change the argument, even though it is not necessary from the caller's point of view for it to make that promise.

    In this case though it gets tricker because once pointers are involved there are more things that can be const or not. In fact I'm a little hazy on this myself, but I think the problem comes from your definition saying that it might alter the pointers in that array, though it promises to not change the data that those pointers pointed to. This actually is a bit of a hollow promise and therein lies the problem.

    Interstingly, the following is actually okay:
    Code:
    int checkargs(const char * const argv[], int *offset, int *length, int *option, const int argc);
    i.e. Adding another const can fix it rather than removing a const.
    Aint C++ grand!
    Last edited by iMalc; 07-11-2011 at 01:25 AM.
    stahta01 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Warning] Incompatible pointer type
    By dgs012 in forum C Programming
    Replies: 5
    Last Post: 02-20-2011, 10:27 AM
  2. Replies: 6
    Last Post: 11-24-2010, 09:46 AM
  3. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM
  4. warning: assignment from a incompatible pointer type
    By enderandrew in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 04:07 AM

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