Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1279
  • Last Modified:

converting char to char *

All,

I am trying to take an array of arguments from char **argv and combine them in one string.

I keep receiving the following error: error C2664: 'strcat' : cannot convert parameter 1 from 'char' to 'char *'
Attached is the code I am using.

Anyone know of a way I can accomplish the above?

Thanks
int main(int argc, char **argv) 
{
	char *sendbuf = "";
	for(int i = 1; i < argc; i++) 
	{
		if((i > 3) && (i < argc))
		{
			strcat(*sendbuf, argv[i]); //Line with error
		}
		printf("Argument: %s\n", argv[i]);
	}
}

Open in new window

0
tr57285
Asked:
tr57285
  • 2
  • 2
1 Solution
 
Infinity08Commented:
sendbuf is a char*
*sendbuf is a char ('\0' in this case)

You cannot strcat to a string literal anyway.

What you should do, is allocate enough memory to hold all command line arguments, and then you can strcat them together :

int main(int argc, char **argv) 
{
        char sendbuf[1024] = "";
        for(int i = 1; i < argc; i++) 
        {
                if((i > 3) && (i < argc))
                {
                        strcat(sendbuf, argv[i]);
                }
                printf("Argument: %s\n", argv[i]);
        }
        return 0;
}

Open in new window

0
 
jkrCommented:
>>I am trying to take an array of arguments from char **argv and combine
>>them in one string.

Since you are apparently on Windows: Just call 'GetCommandLine()' (http://msdn.microsoft.com/en-us/library/ms885605.aspx) to get the full command line as one string. E.g.
#include <windows.h>
 
int main(int argc, char **argv) 
{
        char *sendbuf = GetCommandLine();
 
  return 0;
}

Open in new window

0
 
tr57285Author Commented:
jkr,

Thanks, but now I am receiving the following error:

error C2440: 'initializing' : cannot convert from 'LPWSTR' to 'char *
0
 
jkrCommented:
Ah, you have UNICODE set - in this case, use
#include <windows.h>
 
int main(int argc, char **argv) 
{
        char *sendbuf = GetCommandLineA(); // explicitly call ANSI version
 
  return 0;
}

Open in new window

0
 
tr57285Author Commented:
Thanks jkr, That fixed it!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now