|
-
Jan 10th, 2012, 11:24 AM
#1
Thread Starter
Fanatic Member
Convert char* to char[] ?
I know this should be easy but I can't figure it out, how can I convert a char* to a char[] array? I'm not using CLR or MFC.
Here's what I have so far, using main ( int argc, char* argv[] )
char inchar[33];
char tempchar = 'a'; /* dummy value to start */
int count = 0;
while ( tempchar != '\0' )
{
tempchar = argv[1][count];
inchar[count] = tempchar;
count = count + 1;
}
fprintf(stdout, inchar);
When I run it like PROG.EXE DOG
I get only D as the output ( the first character only every time ).
-
Jan 10th, 2012, 01:54 PM
#2
Re: Convert char* to char[] ?
I compiled and ran your code with no problems, it printed "Dog".
There is actually no real difference between an array of char or a pointer to char, so no conversion should be necessary.
Have you debugged the application and stepped through the loop to see if you can gather any clues as to why its behaving like that?
-
Jan 10th, 2012, 02:12 PM
#3
Thread Starter
Fanatic Member
Re: Convert char* to char[] ?
Weird. Its Visual Studio 2008, a console application ( not CLR or MFC ), this is the exact code, I put the print out of the argument right below the main:
int _tmain(int argc, char* argv[])
{
fprintf(stdout, argv[1]);
...
Typing PROG.EXE DOG still shows just D
-
Jan 10th, 2012, 02:15 PM
#4
Thread Starter
Fanatic Member
Re: Convert char* to char[] ?
Oh Got it. When I created the Application it was something like this:
int _tmain(int argc, _TCHAR* argv)
Changing it to the old way worked ( I had to change the function name too from _tmain to main )
int main(int argc, char* argv[])
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|