Pointer error.

This is a discussion on Pointer error. within the C Programming forums, part of the General Programming Boards category; Hello, I just have a quick question to ask... I am getting an "assignment from incompatible pointer type" in my ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    13

    Pointer error.

    Hello,

    I just have a quick question to ask...

    I am getting an "assignment from incompatible pointer type" in my code and I can't understand why.

    I have actually got this program to compile and run without any errors now, but I would like to know the reason for the problem that was occuring, if anyone could help me out?

    The program is very simple. It asks the user to input a string and uses a function for the inputting. I am still learning pointers so this is only for my own learning purposes.

    Here is the bugged code.

    Code:
    #include <stdio.h>
    
    main()
    {
    	char input[50];
    	char *ptrInput;
    	
    	ptrInput = &input;
    	
    	puts("Enter a string: ");
    	
    	my_scanf(ptrInput);
    	
    	puts(input);
    	
    	fflush(stdin);
    	getchar();
    }
    
    my_scanf(char *ptrInput)
    {
    	gets(ptrInput);
    }
    Here is the working code. Can anyone tell me why a cast is required when I'm using the same type?
    Code:
    #include <stdio.h>
    
    main()
    {
    	char input[50];
    	char *ptrInput;
    	
    	ptrInput = (char *)&input;
    	
    	printf("Enter a string: ");
    	
    	my_scanf(ptrInput);
    	
    	puts(input);
    	
    	fflush(stdin);
    	getchar();
    }
    
    my_scanf(char *ptrInput)
    {
    	gets(ptrInput);
    }
    Thanks.
    Last edited by george_1988; 09-04-2008 at 08:54 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    input is a char[50]. &input, then, is a pointer to an array. A char* is a pointer to a char. Since an array is not a char, you have an assignment from an incompatible pointer type.

    The correct solution is to use the fact that an array is converted to a pointer to its first element when passed as an argument. I would expect something like this:
    Code:
    #include <stdio.h>
    
    void my_scanf(char *ptrInput);
    
    int main()
    {
    	char input[50];
    
    	puts("Enter a string: ");
    
    	my_scanf(input);
    
    	puts(input);
    
    	getchar();
    
    	return 0;
    }
    
    void my_scanf(char *ptrInput)
    {
    	gets(ptrInput);
    }
    Note that I have removed the fflush(stdin) as it is undefined. I have also given main() and my_scanf() their correct return types (though you may or may not want to return something from my_scanf()), and declared a prototype for my_scanf().

    Also, note that you should not be using gets() as will allow a buffer overrun. I presume that you will changing the implementation of my_scanf() to something more sane. You probably also need to change my_scanf() to take the maximum length of the string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Thanks for clearing that up

    Also, I've just been using fflush(); getchar(); because that's what I was shows to do from the beginning.

    I've been following the C programming tutorials by Mark Virtue.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    actualyl &input isnt an array its a pointer to a pointer to an array, so you have incompatible levels of indirection. You shoudl use the line -
    Code:
       ptrInput = (char*)input;
     
    or
     
       ptrInput = (char*)&input[0];
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    977
    Quote Originally Posted by abachler View Post
    actualyl &input isnt an array its a pointer to a pointer to an array, so you have incompatible levels of indirection. You shoudl use the line -
    Code:
       ptrInput = (char*)input;
     
    or
     
       ptrInput = (char*)&input[0];
    The cast it not at all necessary. The cast was being used in the original code to silence a warning (this is usually not the right way to fix such a warning), but since input has type char* (effectively—I know it's really an array), the cast is superfluous.

    Also, given
    Code:
    char input[50];
    it's correct to say that &input is not an array, but neither is it a pointer to a pointer to an array. Laserlight was correct in saying it's a pointer to an array. So you can have something like:
    Code:
    char input[50];
    char (*p)[50]; /* p is a pointer to an array of 50 char */
    p = &input; /* thus this is compatible */

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    In C it is superfluous, but 99.99% of the code I write is C++ so I always cast, which is not incorrect in C. It also makes the code more portable if you decide to port it to C++.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by george_1988 View Post
    I've been following the C programming tutorials by Mark Virtue.
    Perhaps it's time to switch?
    Also, here's more information on gets and avoiding it: http://cpwiki.sourceforge.net/Gets

    Quote Originally Posted by abachler View Post
    In C it is superfluous, but 99.99&#37; of the code I write is C++ so I always cast, which is not incorrect in C. It also makes the code more portable if you decide to port it to C++.
    But the point was that it's perfectly valid in both C and C++ without a cast because you don't use & on the array, it simply decays to a char* pointer to which you assign it.
    Last edited by Elysia; 09-05-2008 at 01:22 AM.
    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.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    As mentioned already, just not sure on your intent to allocate a 50 char array to simply pass it to a pointer...a nono. I would advise using malloc for char *ptrinput to allocate room. Then use fgets() over scanf to get your data from the user.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > C programming tutorials by Mark Virtue

    "taught by an expert C programmer, Mark Virtue, who has been using C for over 15 years, and has been teaching C programming for over 5 years. "
    If he's pumping out garbage with gets() and fflush(stdin), that's a hell of a lot of brain-rot.

    This one will be much better
    http://www.eskimo.com/~scs/cclass/cclass.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by abachler View Post
    actualyl &input isnt an array its a pointer to a pointer to an array.
    No it is not.

    input is an array of 50 char. &input is the address of an array of 50 char (of type pointer to array of 50 char).

    Quote Originally Posted by abachler View Post
    Code:
       ptrInput = (char*)input;
     
    or
     
       ptrInput = (char*)&input[0];
    These two lines are equivalent. In the first line, the name of the array is implicitly converted to a pointer to the first character. The second line does that explicitly.

    In both cases, the conversion to char * is not required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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