bingie
asked on
Pass a Char Array to a pointer in a function
I've specified the function definition in the header file (strv.h):
void strRev(char* theString);
Here is my function:
#include "strv.h"
void strRev(char* theString){
char *pEnd = theString;
char temp;
while (*pEnd) pEnd++;
pEnd--;
while (theString < pEnd) {
temp = *pEnd;
*pEnd-- = *theString;
*theString++ = temp;
}
}
Now, I'm trying to call this from main, but getting an error:
#include "strv.h"
#include <iostream.h>
void main(){
char sArr[7] = "abcdef";
strRev(&sArr[0]);
cout<<sArr<<endl;
}
I'm getting the following error:
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Str2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
What is this??
void strRev(char* theString);
Here is my function:
#include "strv.h"
void strRev(char* theString){
char *pEnd = theString;
char temp;
while (*pEnd) pEnd++;
pEnd--;
while (theString < pEnd) {
temp = *pEnd;
*pEnd-- = *theString;
*theString++ = temp;
}
}
Now, I'm trying to call this from main, but getting an error:
#include "strv.h"
#include <iostream.h>
void main(){
char sArr[7] = "abcdef";
strRev(&sArr[0]);
cout<<sArr<<endl;
}
I'm getting the following error:
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Str2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
What is this??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or take that: (i tried to remove pointer arithmetics as it isn't recommended in C++).
void strRev(char theString[], int len);
#include "strv.h"
void strRev(char theString[], int len)
{
char temp;
for (int i = 0; i < len/2; ++i)
{
temp = theString[len-1-i];
theString[len-1-i] = theString[i];
theString[i] = temp;
}
}
#include "strv.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char sArr[] = "abcdefg";
strRev(sArr, sizeof(sArr)-1);
AfxMessageBox(sArr);
return 0;
}
Regards, Alex
void strRev(char theString[], int len);
#include "strv.h"
void strRev(char theString[], int len)
{
char temp;
for (int i = 0; i < len/2; ++i)
{
temp = theString[len-1-i];
theString[len-1-i] = theString[i];
theString[i] = temp;
}
}
#include "strv.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char sArr[] = "abcdefg";
strRev(sArr, sizeof(sArr)-1);
AfxMessageBox(sArr);
return 0;
}
Regards, Alex
FWIW... Nice also to consider using the iterator constructor with reverse iterators from std::string.
--------8<--------
#include <string>
#include "windows.h"
#pragma comment(lib,"user32.lib")
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
std::string sArr = "abcdefg";
MessageBox(NULL,std::strin g(sArr.rbe gin(),sArr .rend()).c _str(),"", MB_OK);
return 0;
}
--------8<--------
--------8<--------
#include <string>
#include "windows.h"
#pragma comment(lib,"user32.lib")
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
std::string sArr = "abcdefg";
MessageBox(NULL,std::strin
return 0;
}
--------8<--------
I think AlexFM solved your problem, but these kinds of programs should be created as
Visual C++ Projects -> Console Application (in MS visual studio 2003)
Then you get an "int _tmain()" instead of the "int APIENTRY WinMain()" or whatever
You might also want to try the char * strrev(char *) function from <string.h> (Most operations on char arrays have been solved there.)
It reverses a null terminated string (no need to input the length) and returns a pointer to it;
#include <iostream.h>
#include <string.h>
void main(){
char sArr[7] = "abcdef";
strrev(&sArr[0]);
cout<<sArr<<endl;
}
// the only real difference between &sArr[0] and sArr is that its very hard to forget that &sArr[0] is a pointer
Visual C++ Projects -> Console Application (in MS visual studio 2003)
Then you get an "int _tmain()" instead of the "int APIENTRY WinMain()" or whatever
You might also want to try the char * strrev(char *) function from <string.h> (Most operations on char arrays have been solved there.)
It reverses a null terminated string (no need to input the length) and returns a pointer to it;
#include <iostream.h>
#include <string.h>
void main(){
char sArr[7] = "abcdef";
strrev(&sArr[0]);
cout<<sArr<<endl;
}
// the only real difference between &sArr[0] and sArr is that its very hard to forget that &sArr[0] is a pointer
I just wanted to say that, in your main function, instead of :
strRev(&sArr[0]);
you should use
strRev(sArr);
Which gives exactly the same result, but aren't the laziest devs the better ? :p
Glom