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 SkipVought 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
0
0
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>&nbsp;void main(void)<br>&nbsp;{ int x = 36;<br>&nbsp;&nbsp;&nbsp;Trial(x);&nbsp;&nbsp;&nbsp;<br>&nbsp;} <br><br>&nbsp;void Trial(int *y)<br>&nbsp;{ cout &lt;&lt; y &lt;&lt; endl;<br>&nbsp;}<br><br><br>ERROR:<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.
 
<br>When you say that the parameter is a pointer, then you are supposed to pass the address of the same type. For eg. I 've changed the code, please test and let me know whether it worked or not.<br><br>void Trial(int *);&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void main(void)<br>&nbsp;&nbsp;&n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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>Thanx<br>Siddhartha Singh<br><A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
#include&lt;iostream.h&gt;<br><br>void Trial(int *y); <br>void main()<br>&nbsp;{ <br>&nbsp;&nbsp;int x = 36;<br>&nbsp;&nbsp;Trial(&x);&nbsp;&nbsp;// pass the address of x<br>&nbsp;} <br><br>void Trial(int *y)<br>&nbsp;{ <br>&nbsp;&nbsp;&nbsp;cout &lt;&lt; *y &lt;&lt; endl; // print the value of y. Is that what you want?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// cout &lt;&lt; y &lt;&lt; endl; will print out the address of y<br>&nbsp;}
 
Just incase you didnt catch what they mean<br>a pointer does nothing more than point to the address of a location in memory, and the pointer type lets the compiler know how to interpret the memory pointed to, the Amperstand & stands for &quot;Address of&quot; when the function is expecting a pointer, it makes its local copy of pointer variable *y , which is pointing to the &quot;address of&quot; X, not the Value of X.<br><br>also the function prototype has to appear exactly the same as the function definition(except in optional parameters)<br><br>the last two was right, I'm just offering an in english explanation. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Thanks, Karl... That's exactly what I needed... <br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top