Thread: ISO c++ forbids comparisson btween pointers and integers

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

    Exclamation ISO c++ forbids comparisson btween pointers and integers

    Hello again.
    So im trying to compile a programm and i get the following message
    "ISO c++ forbids comparisson between pointers and integers"
    my code were the problem occurs is the following :
    Code:
    void find()
    {
    	int num;
    	printf("\n Parakalo doste enan akaireo arithmo : "); //give an integer
    	scanf("%d",&num);
    	for ( int i=0; i<6 ; i++) /*searches an array of 5 inetgers to find if the user's integer is inside the array*/
    	{
    		if (ptrF==num) /*note: ptfF is a pointer. it is int *ptrF=A...A is the array*/
    		{
    			printf("\n I timi yparxei ston pinaka sas !");
    		}
    		if ( ptrF!=num) //change the pointer to +1 to check the next one
    		{
    			ptrF++;
    		}
    	}
    }
    i cant find why it is not working, since the pointer is pointing to an int array.
    Isnt it pointing to the first elemen of the array??
    and when i say ptrF it should go to the next one right??

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    You need to dereference your pointer. Put a * in front of it to dereference it.

    Right now, you are trying to compare the integer to the address that the pointer holds, not the actual value stored at that address.

    edit: Wait a minute..pointer to an array, I missed that. What I just wrote might not be true. I still think it's a dereference problem though, maybe you need to dereference it with []?
    Last edited by Walle; 01-10-2010 at 01:48 PM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    i still cant make it work :S
    the program has to put integer into an array witht he use of pointers.
    and also it should be able to check if there is an integer into the array, that the user wants ti find.
    this is the whole code:
    Code:
    #include <stdio.h>
    
    
    int A[5];
    int *ptrI=A,*ptrF=A;
    
    void insert(),find();
    void insert()  //user inserts vallues into the array
    {int value;
    	for ( int i=0; i<5 ; i++)
    	{
        printf("\n Doste enan akaireo arithmo : ");
    	scanf("%d",&value); //users value stored here
    	*ptrI=value; //value inserted with the use of pointer into the array
    		ptrI++; //pointer moves to the next array element
    }
    }
    void find() //finds a value
    {
    	int num;
    	printf("\n Parakalo doste enan akaireo arithmo : ");
    	scanf("%d",&num); //users value stored into num
    	for ( int i=0; i<5 ; i++) //scans the array. if users value= value of array element it prints the message. otherwise it goes into the next element(ptrF++)
    	{
    		if ( *ptrF==num)
    		{
    			printf("\n I timi yparxei ston pinaka sas !");
    		}
    		if ( *ptrF !=num)
    		{
    			ptrF++;
    		}
    	}
    }
    
    
    
    int main()
    {
    	int ex=0;
    do 
    {
    int opt;
    printf("\n 1.Eisagogi Stoixeiwn : ");
    printf("\n 2.Euresi Stoixeiwn : ");
    printf("\n 3.Telos programmatos : ");
    scanf("%d",&opt);
    if (opt==1)
    {
    insert();
    }
    if (opt==2)
    {
    find();
    }
    if (opt==3) // exit
    {
    	ex=1;
    }
    }while (ex!=1);
    }

    the problem is that i am not familiar witht he use of pointers, and i cant get it right :S
    any help is accepted as i have to deliver this to the university by e-mail by the end of the day, wich is 45 min away :P

    i have sended it with this code, since i cant fix it, but i can resend it if i get the code right before midnight
    Last edited by Cursius; 01-10-2010 at 03:21 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since the pointer points to an array, consider this:
    An array is collection (or vector, if you will) of values. So when you try to compare that array there, WHICH of those values did you want to compare? The compiler doesn't know, and gives you an error.
    Now, letting a pointer point to an array makes it point to its first element. So if you dereference the pointer, it would be the same as writing name_of_array[0]. But I don't think you want to compare to the 0th element for every time in the loop?
    Instead, you would try every element in the array, yes? Then you would have to tell the compiler which element you want to compare by putting name_of_pointer[element].
    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.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    k will try it
    does not count for my grade as the deadline is off, but it would rly help me to understand pointers better

    "Thanks Elysia. You're a programming master! How the hell do you know every thing?"

    :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM