CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2014
    Posts
    2

    "Invalid conversion from int to *int"

    So I'm trying to learn how to use pointers, but my compiler has decided I'm not allowed to. Every time I try to assign anything to a pointer I get this error:
    Code:
    error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
      pNumber = number;
    This is my code.
    Code:
    #include <iostream>
    
    int main()
    {
    	int number = 6;
    	
    	int * pNumber;
    
    	pNumber = number;
    
    	return 0;
    }
    What am I doing wrong here?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: "Invalid conversion from int to *int"

    You need to take the address:
    Code:
    pNumber = &number;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2014
    Posts
    2

    Re: "Invalid conversion from int to *int"

    Knew I'd be doing something stupid like that, thanks!

  4. #4
    Join Date
    Feb 2014
    Location
    India
    Posts
    5

    Re: "Invalid conversion from int to *int"

    A pointer can only store the address of the partilcular variable location.
    It cannot store the content.

    Hope i could help...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured