String handling in C programming...accepting arguments from the command line.

Basically I am trying to accept an argument from the command line and use that argument as a part of my file name. I want to create a file called OUTXX where XX is the command line argument (I want to accept this only in the command line). I wrote a small test code for this..

I want to concatenate the argument with "out" string and store the resultant in a char array called 'file'. Then I would use this char array in my fopen statement.
But I am getting an error called 'incompatible types in assignment'. I don't understand why. argv[1] points to the first argument after the executable name and "out" is a string. Why can't it concatenate both? Please advice if I can achieve in a better method.

Popular White Paper On This Topic

Several issues.
First, you might know what type of arguments strcat() takes, but the compiler does not. You need to #include <string.h> so the compiler sees a function prototype.
Second, do not do the assignment file=strcat. Strcat appends the second string to the first. The first string "out" is a string literal somewhere in memory. You can't just append to it - the added characters will end up in your constants area, not in the variable "file".
Third, strcat() returns the address of the whole string as a courtesy - you could use it to initialize another pointer or whatever. But it will not copy the result. In C, a string is not a basic type that the compiler knows about - that is why there are string functions to use.
Fourth, if the user puts in more than 6 characters he will crash your code because the whole thing will not fit in the variable.
Fifth, if the user does not enter a filename, he will crash your code because agrv[1] will be a null pointer.
I think you want:

On the fourth point, I would recommend using something like snprintf, which will take the buffer size and ensure no overruns. I would further recommend you pass in ( buffersize - 1 )
as the buffer size, and set your last character in the buffer to zero.

The problem is, that argv[] is an array of pointers. If you say argv[1], then you talk about the second address (because the first address contains the name of the function itself). But if you say *argv[1], then you mean the string where that address points to. Adding the asterix * may solve your problem.

In general, and especially where there is a mix of string, integer, float etc. to be formatted, I would agree sprintf offers a lot of readability and simplicity.

Couple of reservations, however:

(a) There was a time when sprintf was unreliable with long operands - I seem to remember 256-byte total buffer limit. I don't know if any of those systems are still out there.

(b) Because sprintf uses a generic formatting string, you don't benefit from any checks on the function prototype: the varargs are just assumed to be a corresponding array of long ints, doubles, and char *s. Very common cause of segviol, printf and sprintf.

(c) Because it's general, sprintf is slower. A strcat doesn't have to parse the format string on each use, and the compiler can inline the function and even use a processor instruction rather than the library, if available.

Hi, char *argv[] is pointer array that points to the character but char
file[10] is only character array ,it is not a pointer to character array. So
it may be the reason when assigning we get incompatible type error. Thank you
for posting.

char *strcat (char *dest, const char *src);So you have to copy "out" first in your source string then you have to do strcat char file[30];strcpy(file, "out");strcat(file, argv[1]);Hope you understand what the issue in your code.

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.