Help with pointers please

This is a discussion on Help with pointers please within the C Programming forums, part of the General Programming Boards category; I am only just learning pointers and finding them quite difficult. When I try to compile I get pointers.c:18: error: ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Help with pointers please

    I am only just learning pointers and finding them quite difficult. When I try to compile I get pointers.c:18: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token. I am not to sure what this means.

    Is it ok for me to do this?

    int alpha;

    getAlpha(*alpha);

    void getAlpha(int &alpha)


    .... rest of code.

    Sorry if this doesn't make sense

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    No, you have your * and your & mixed up.

    Please use code tags next time.
    Code:
    void getAlpha(int *alpha)
    {
      *alpha = 5;
    }
    
    void foo(void)
    {
      int alpha;
    
      getAlpha(&alpha);
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    (use code tags)

    I dont understand exactly what you are trying to do...

    But if you want to pass pointer as argument to your function, then let me explain:
    Code:
    int getAlpha(int *alpha)
    {
    	*alpha = 1;
    
    	return 0;
    }
    
    int main( void )
    {
    	int alpha = 0;
    
    	if( !getAlpha( &alpha ) )
    	{
    		printf( "Got alpha: %d\n", alpha );
    	}
    	else
    	{
    		printf( "Error getting alpha!\n" );
    	}
    
    	return 0;
    }
    Sorry for being pedantic
    You initialize variable alpha which is just an int variable. Then with &alpha you get it's pointer (reference) which you can pass to getAlpha() function as argument since it requires pointer *.

    In function we assign pointer * alpha int value 1, so we need to dereference the pointer: *alpha = 1;

    And about me being pedantic, every function should return value indicating it's success and caller function should handle that return value, in one way or another. Just couldnt write simple example without doing it this way.

    So, int he basics; &var is the reference to var. *var is dereferenced var.
    That's why if you have int and you want to make pointer pointing to it, you type:
    Code:
    int var = 0;
    
    int *pvar = &var;
    // OR
    int *pvar;
    pvar = &var;
    // then use pointer to change var's value from 0 to 1:
    *pvar = 1;
    So, pointer pvar points to variable var. Reference & is almost the same thing as pointer to it's variable. So, it's possible to use variable var = 1 or *&var = 1 since * dereferences the & reference.

    Spooky aint it?
    Last edited by Rennor; 08-24-2006 at 11:35 PM.

  4. #4
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry I forgot to put the tags on!

    Thanks, I will have another try now and see how it goes.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    10
    No, in your getAlpha() you are trying to de-reference a pointer but you did not decalre alpha as a pointer.
    A simple example may help -
    Code:
    #include <stdio.h>
    
    /* declare your function*/
    void getAlpha(int &alpha){
    	printf("Alpha's value = %d \n",alpha);
    }
    
    int main(int argc, char* argv[])
    {
    	/* decalre and initialise a normal int var.*/
    	int alphavalue = 6;
    
    	 /* declare a pointer that points to an int (holds its address) 
    	 but currently  holds nothing and will crash your app if you don't 
    	 point it to something before using it!*/
    	int * alpha = NULL;
    
    	/*pass the address of the normal int, now the pointer 
    	actually points to something, very important!*/
    	alpha = &alphavalue;
    
    	/* use your function, now you can dreference your pointer */
    	getAlpha(*alpha);
    	return 0;
    }
    <edit> wow, you guys are fast, nearly half a dozen posts while I was writing mine!

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I'm still playing with it but I just can't work it out sorry.

    This all the code. Ignore the logic errors I'm still playing with those.

    Code:
    #include <stdio.h>
    
    int main()
    {
       int alpha;
       int *alpha = NULL;
       alpha = &alpha
    
       getalpha(*alpha);
    
       if (ispointers(alpha)) 
          printf("\n%d COMMENT HERE\n", alpha);
       else 
          printf("\n%d OPP COMMENT HERE\n", alpha);
    
       return 0;
    }
    
    void getalpha(int &alpha)
    {
       printf("INSTRUCTION ");
       if (scanf("%d", alpha) != 2 || *alpha < 0)
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    int ispointers(int alpha)
    {
    
       int pal, temp = alpha;
    
       while (temp > 0)
       {
          pal = pal / 10 + temp % 10;
          temp /= 10;
       }
    
       return (pal = alpha);
    }
    Thanks guys.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    10
    int alpha and int* alpha are the same (or close enough to confuse the compiler when you use alpha somewhere else) so you can as a identifier use a p in from of your pointer def's like so

    int* pAlpha;

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
       int alpha;
       int *alpha = NULL;
       alpha = &alpha
    How many alphas are you trying to declare exactly? variables in the same scope have to have unique names. Maybe call the pointer int *palpha or something.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ahhh ok I've fixed it up. I think I understand it now.

    Thanks alot for you're help. (",)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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