Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers

Status
Not open for further replies.

Dena

Programmer
Apr 18, 2000
6
US
Hi, all... I'm a student taking my first C++ class.&nbsp;&nbsp;The semester's almost over & I've<br>written a complex program (well, complex to me!) that is giving me some headaches.&nbsp;&nbsp;<br><br>The following code is a -very- simplified version of a portion of my program code that is giving me the same error.&nbsp;&nbsp;I'm passing char strings (two-dimensional) by pointer, but C++ doesn't like how I've done it... <br><br>I also get an error when I use (strcpy). Am I supposed to use [] or row/column info when in a strcpy command? I've tried it with & without in several different ways, but no luck.&nbsp;&nbsp;The error reads something like &quot;cannot convert char * to const char *&quot;... What I'm trying to do is alphabetically sort by bubble sort.<br><br>please help me understand why this simple little code isn't working... thanks!!<br><br>&nbsp;void Trial(int *);&nbsp;&nbsp;&nbsp;<br><br>void main(void)<br>{ int x = 36;<br>&nbsp;&nbsp;Trial(x);&nbsp;&nbsp;&nbsp;<br>} <br><br>void Trial(int *y)<br>{ cout &lt;&lt; y &lt;&lt; endl;<br>}<br><br><br>ERROR:<br><br>error C2664: 'Trial' : cannot convert parameter 1 from 'int' to 'int *'. <br>Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
 
Your defined the type of parameter y in function void Trial to be a pointer to an integer while passing an integer value instead of its pointer when calling the function in the line:<br><br>Trial(x);<br><br>To fix the problem, pass the address of x as follow:<br><br>Trial(&x);<br><br>and the Trial function should be rewritten as:<br><br>void Trial (int *y)<br>{<br>cout &lt;&lt; *y &lt;&lt; endl;<br>}<br><br>On the other hand, since you was using C++, use reference instead of pointer as parameter type in function Trial as:<br><br>void Trail (&y)&nbsp;&nbsp;/* &quot;&&quot; instead of &quot;*&quot; */<br>{<br>cout &lt;&lt; y &lt;&lt; endl;<br>}<br><br>and that will fix the problem as well.<br><br><br>&nbsp;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top