Solved

Pass a Char Array to a pointer in a function

Posted on 2004-09-27
5
2,958 Views
Last Modified: 2008-01-09
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??
0
Comment
Question by:bingie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12159157
Your code is OK, but you have a problem with application type. Your application has main function, this means, linker option should be: /SUBSYSTEM:CONSOLE. Currently you have  /SUBSYSTEM:WINDOWS, and linker needs WinMain function for this. If you use VC++ 6, change /subsystem:windows to /subsystem:console in Project - Settings - Link - Project options.
You can create new project of type "Console Application" and paste this code to it.
0
 
LVL 4

Expert Comment

by:Glom
ID: 12159659
I think AlexFM solved your problem.
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
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12162283
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




0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12198425
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::string(sArr.rbegin(),sArr.rend()).c_str(),"",MB_OK);
     return 0;
}
--------8<--------
0
 
LVL 4

Expert Comment

by:bytta
ID: 12199019
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
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question