I am trying to use va_list & its associated macros with vsprintf() to create a format string that has a variable number of specifiers. Here is an example program I wrote in which the number of specifiers can only be altered via the NUM_ARG macro:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define MAXBUF 4096
#define SPECIFIER "(%s)"
#define NUM_ARG 5
char *strmaker(int num_args, ...)
{
char form[MAXBUF] = { [0] = '\0' };
char *prnt = (char *) malloc(sizeof(char) * MAXBUF);
va_list strings;
for (int i = 0; i < num_args; ++i)
strcat(form, SPECIFIER);
va_start(strings, num_args);
vsprintf(prnt, form, strings);
va_end(strings);
return prnt;
}
int main(int argc, char *argv[])
{
if (argc != (NUM_ARG + 1))
return -1;
char *s = strmaker(NUM_ARG, argv[1], argv[2], argv[3], argv[4], argv[5]);
printf("%s\n", s);
free(s);
return 0;
}
However, this isn't exactly what I want to achieve. How could I do this with a variable number of arguments? How could a variable number of strings be passed to a function and used to initialise a va_list?
As far as I know, it is not possible to do that. If you are not so keen about using variadic functions and can redefine the function. The below code suits your need; Iterate through each item in the array and append to the string using snprintf.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXBUF 4096
#define SPECIFIER "(%s)"
char *strmaker(int num_args, char** strings)
{
char *prnt = (char *) malloc(sizeof(char) * MAXBUF);
int cur = 0;
/* Append the strings to the prnt buffer */
for (int i = 0; i < num_args; i++) {
int p_return = snprintf(prnt + cur, MAXBUF - cur, SPECIFIER, strings[i]); // If no error, return the number characters printed excluding nul (man page)
if (p_return >= MAXBUF - cur) // If buffer overflows (man page)
return prnt;
cur = cur + p_return; // Update the index location.
}
return prnt;
}
int main(int argc, char *argv[])
{
if (argc <= 1)
return -1;
char *s = strmaker(argc - 1, argv + 1);
printf("%s\n", s);
free(s);
return 0;
}
Terminal Session:
$ ./a.out 1 2 3
(1)(2)(3)
$ ./a.out 1 2 3 4 5 6 7
(1)(2)(3)(4)(5)(6)(7)
$ ./a.out Hello, This is stackoverflow, Bye
(Hello,)(This)(is)(stackoverflow,)(Bye)
Short answer is: You can't.
However you can work around it by using arrays of strings, possibly dynamically allocated. Then you could basically use the same technique you do now, but iterate over the array instead.
Perhaps something like this:
char *strmaker(size_t count, char *strings[])
{
// First get the length of all strings in the array
size_t result_length = 0;
for (size_t i = 0; i < count; ++i)
{
// +1 for space between the strings
// And for the last string adds space for the string null-terminator
result_length += strlen(strings[i]) + 1;
}
// Now allocate the string (using calloc to initialize memory to zero, same as the string null-terminator)
char *result = calloc(1, result_length);
// And not concatenate all strings in the array into one large string
for (size_t i = 0; i < count; ++i)
{
strcat(result, strings[i]);
if (i != count - 1)
{
strcat(result, " "); // Add space, except after last string
}
}
// Return the resulting string
return string;
}
int main(int argc, char *argv[])
{
// Create an array for all arguments
char **arguments = malloc(sizeof(char *) * argc - 1);
for (int a = 1; a < argc)
{
arguments[a - 1] = argv[a];
}
// Now create the single string
char *result = strmaker(argc - 1, arguments);
// ... and print it
printf("%s\n", result);
// Finally clean up after us
free(result);
free(arguments);
}
For the command-line arguments in argv you don't really need to create a new array to hold them, but it showcases how to create an array of string to pass to strmaker. You can use any strings you want instead of the command-line arguments.
Related
In C programming language, is it possible to access int argc or char **argv without using the parameters? I know some of you might ask why this is needed, just for research purposes.
Is it possible to generate the cmd line arguments without using the main parameter variables ? For example, to illustrate some pseudo code, that i have in mind,
LPTSTR cmd = GetCommandLine();
splitted = cmd.split(" ") //split from spaces
char **someArgv.pushForEach Splitted, length++
and you'd have a someArgv with the parameters and length as argc, this'd really help to know if possible to illustrate.
If OP already has the command as a string, then:
Form a copy of the string
Parse it for argument count
Allocate for argv[]
Parse & tokenize copy for each argv[]
Call main()
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// Not standard, but commonly available
char *strdup(const char *s);
// Return length of token and adjust offset to the next one
// Adjust as needed
// Presently only ' ' are used to separate
// More advanced would have escape characters, other white-space, etc.
size_t tokenize(const char *s, size_t *offset) {
// find following space
size_t len = strcspn(s + *offset, " ");
*offset += len;
// find first non-space
*offset += strspn(s + *offset, " ");
return len;
}
int call_main(const char *cmd) {
char *cmd2 = strdup(cmd);
cmd2 += strspn(cmd2, " "); // skip leading spaces
size_t offset = 0;
int argc = 0;
while (tokenize(cmd2, &offset) > 0) {
argc++;
}
char **argv = malloc(sizeof *argv * ((unsigned)argc + 1u));
offset = 0;
for (int a = 0; a < argc; a++) {
argv[a] = &cmd2[offset];
size_t len = tokenize(cmd2, &offset);
argv[a][len] = '\0';
}
argv[argc] = NULL;
int retval = 0;
#if 0
retval = main(argc, argv);
#else
printf("argc:%d argv:", argc);
for (int a = 0; a < argc; a++) {
printf("%p \"%s\", ", argv[a], argv[a]);
}
printf("%p\n", argv[argc]);
#endif
free(cmd2);
free(argv);
return retval;
}
Sample
int main() {
call_main(" name 123 abc 456 ");
}
argc:4 argv:0x800062322 "name", 0x800062327 "123", 0x80006232c "abc", 0x800062331 "456", 0x0
Pedantic: The strings provided to main() should be modifiable. Avoid code like
argv[1] = "Hello";
....
main(argc, argv);
#include <stdio.h>
int main(int argc, char *argv[]);
int callMain(void)
{
char *argv[4];
argv[0] = "binary";
argv[1] = "param1";
argv[2] = "param2";
argv[3] = NULL;
return main(3, argv);
}
int main(int argc, char *argv[])
{
if (argc <= 1)
{
return callMain();
}
printf("ARGC: %u\n", argc);
int i;
for (i = 0; i < argc; i++)
printf("ARG: %u - %s\n", i, argv[i]);
return 0;
}
Why doesn't this code work? What I have been trying to do is make a dynamic allocation for an unknown user input length of an array using int main(int ac, char ** ac) and malloc().
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
char *str;
int i;
str = malloc(sizeof(char) * (ac + 1));
i = 0;
if(str[i] != '\0')
{
i = i + 1;
printf("%s\n\t", str);
}
return(0);
}
Name the parameters of main() in the standardized way: int argc, char *argv[].
(note that argv[0] is the name of the executable, which may or may not be of interest to you)
First you need to allocate argc number of pointers to character. Optionally with a NULL sentinel value at the end if that makes sense for your program - don't confuse this with null termination though.
So you should rather have something like char** str_table = malloc(sizeof(*str_table) * argc);
For each item in str_table, allocate additional memory for strlen(argv[i]) + 1 characters, the actual data. In this case the +1 is for null termination and it is mandatory.
strcpy from argv[i] to your own array.
ac is not the length of any argument, but rather the argument count.
When the user specifies one argument this will always be 2.
If the program should only output the first argument you can just do:
#include <stdio.h>
int main(int argc, char **argv) {
if(argc == 2)
printf("%s\n", argv[1]);
return 0;
}
If you want to load the argument into a string you have to get it's length.
This can for example be done with the strlen() function:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for malloc and free */
#include <string.h> /* for strlen and strcpy */
int main(int argc, char **argv) {
if(argc == 2) {
char *input_string;
int input_string_length;
input_string_length = strlen(argv[1]);
/* strlen() does not include the '\0' in the length */
input_string = malloc((input_string_length + 1) * sizeof(char));
strcpy(input_string, argv[1]);
printf("%s\n", input_string);
free(input_string);
}
return 0;
}
ac doesn't represent user input length (as you have been trying to allocate memory for ac+1) + str points to a raw memory location without any valid data, even then if you want to go with ac then:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //added for memset
int main(int ac, char **av)
{
char *str;
int i; //USELESS
str = malloc(sizeof(char) * (ac + 1)); //str points to a memory (if allocated) which contains garbage
i = 0; //NO USE
if(str) //checks if memory has been allocated - NO INCREMENT NEEDED OR VALID
{
printf("Memory has been successfully allocated");
memset(str,'\0',ac+1); //added for NULL termination
}
return(0);
}
In case you are trying to allocate memory on the stack, you may also look for VLA.
This statement
str = malloc(sizeof(char) * (ac + 1));
does not make sense.
Moreover the allocated array was not initialized. So this statement
if(str[i] != '\0')
results in undefined behavior.
It seems what you are trying to do is to output command line parameters by copying them in dynamically allocated arrays.
If so then the program can look the following way
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char * argv[] )
{
if ( argc > 1 )
{
char **s = malloc( ( argc - 1 ) * sizeof( char *) );
for ( int i = 0; i < argc - 1; i++ )
{
s[i] = malloc( strlen( argv[i+1] ) + 1 );
strcpy( s[i], argv[i+1] );
}
for ( int i = 0; i < argc - 1; i++ )
{
puts( s[i] );
}
for ( int i = 0; i < argc - 1; i++ ) free( s[i] );
free( s );
}
return 0;
}
If to run the program like for example
program Hello World
then the output will be
Hello
World
You can add yourself checks to the program that the memory allocations were successful.
There are some logic error in your code.
int ac is the number of arguments.
char **av contain the arguments.
For example : ./program arg1 arg2
av[0] is gonna be "./program"
av[1] is gonna be "arg1"
av[2] is gonna be "arg2"
av[2][0] is gonna be "a"
Now if you just want the size of the first argument you can use strlen() :
int size = strlen(av[1]);
What you need instead of your if statement is a while loop to go throught all the elements of av.
For example :
int i = 0;
while (i <= ac) {
printf("%s", av[i]);
...Your code...
i++;
}
I am writing a program that takes an argument array from the command line(ex. 10 20 30 40), converts them into integers and saves them in an int array to be used later. I have declared a pointer for the heap. I want to store the number count from the CL in the length variable. Then allocate space for the length and copy it to the heap. Next, use a function that converts the command line arguments to an integer and copy them in a int array.I am confused as to how I can pass the command line values. Could someone point me in the right direction? Thanks.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void convert(char** source, int length);
int main(int argc, char *argv[]){
int length = 0;
char *p_heap;
if( argc > 11 || argc < 2 ) {
printf("arguments 1-10 are accepted %d provided\n", argc-1);
printf("Program Name Is: %s",argv[0]);
exit(EXIT_FAILURE);
}
length = argc-1;
p_heap = malloc(sizeof(length));
strcpy(p_heap, length);
convert(p_heap, length);
//printf("Average %f\n", avg());
puts(p_heap);
free(p_heap);
return 0;
}
void convert(char** source, int length){
int *dst;
int i;
for(i=0;i<length;i++) {
dst = atoi([i]);
}
}
Note: I am assuming correct input from CL.
I want to store the number count from the CL in the length variable.
If you assume correct input from CL, then you have this number in argc-1.
Then allocate space for the length and copy it to the heap.
dst = malloc((argc-1)*sizeof *dst);
Next, use a function that converts the command line arguments to an integer and copy them in a int array.
for(int i=0; i<argc-1; i++)
sscanf(source[i], "%d", &dst[i]);
You should also change the return type of convert to int * and then return dst.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv)
{
if (argc == 1)
return EXIT_SUCCESS;
long *data = malloc((argc - 1) * sizeof *data);
for (int i = 1; i < argc; ++i) {
char *endptr;
errno = 0;
data[i-1] = strtol(argv[i], &endptr, 10);
if (*endptr != '\0') {
fputs("Input error :(", stderr);
return EXIT_FAILURE;
}
if (errno == ERANGE) {
fputs("Parameter out of range :(\n\n", stderr);
return EXIT_FAILURE;
}
}
for (int i = 0; i < argc - 1; ++i)
printf("%ld\n", data[i]);
free(data);
}
why the comparison with 11 in if( argc > 11 || argc < 2 ) { ?
in
length = argc-1;
p_heap = malloc(sizeof(length));
sizeof(length) is sizeof(int) and does not depend on the value of length if it was your hope
In
strcpy(p_heap, length);
strcpy get two char*, length values the number of args rather than the address of an array of char, so the result is undefined and probably dramatic
In
convert(p_heap, length);
the first argument of convert must be a char** but p_heap is a char*
in
void convert(char** source, int length){
int *dst;
int i;
for(i=0;i<length;i++) {
dst = atoi([i]);
}
}
you do not use source
dst is a int* while atoi return an int
[i] ???
Before to give code on S.O. I encourage you to check first it compile without warning/error, using high warning level (e.g. gcc -pedantic -Wextra for gcc )
While adding string to my pointer's array, it is being overwriten by the last one. Could anyone tell me, where's my mistake?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main (){
int ile = 3;
const char * slowa[ile];
for(int j = 0; j < ile; j++){
char string[30];
gets(string);
slowa[j] = string;
printf ("%s dodalem pierwsza\n",string);
}
for (int i = 0; i < ile; i++) {
printf ("%s numer %d\n",slowa[i],i);
}
return 0;
}
The answer is in the following two lines of code:
char string[30];
...
slowa[j] = string;
The assignment sets slowa[j] to the address of the same buffer, without making a copy. Hence, the last thing that you put in the buffer would be referenced by all elements of slowa[] array, up to position of j-1.
In order to fix this problem, make copies before storing values in slowa. You can use non-standard strdup, or use malloc+strcpy:
char string[30];
gets(string);
slowa[j] = malloc(strlen(string)+1);
strcpy(slowa[j], string);
In both cases you need to call free on all elements of slowa[] array to which you have assigned values in order to avoid memory leaks.
You're always pointing to array of chars which is stack variable it's locally allocated only in scope of function, possibly each declaration of string will be on the same address as previous iteration in your loop. You could either instead of using array of chars allocate memory each loop iteration or use array and then using i.e strdup allocate memory for your new string like
slowa[j] = strdup(string) :
As others have said, you need to create copies of the strings, otherwise you set the strings to the same address, and therefore they just overwrite each other.
Additionally, I think using fgets over gets is a much safer approach. This is because gets is very prone to buffer overflow, whereas with fgets, you can easily check for buffer overflow.
This is some code I wrote a while ago which is similar to what you are trying to achieve:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PTRS 3
#define STRLEN 30
int
string_cmp(const void *a, const void *b) {
const char *str1 = *(const char**)a;
const char *str2 = *(const char**)b;
return strcmp(str1, str2);
}
int
main(void) {
char *strings[PTRS];
char string[STRLEN];
int str;
size_t len, i = 0;
while (i < PTRS) {
printf("Enter a string: ");
if (fgets(string, STRLEN, stdin) == NULL) {
fprintf(stderr, "%s\n", "Error reading string");
exit(EXIT_FAILURE);
}
len = strlen(string);
if (string[len-1] == '\n') {
string[len-1] = '\0';
} else {
break;
}
strings[i] = malloc(strlen(string)+1);
if (strings[i] == NULL) {
fprintf(stderr, "%s\n", "Cannot malloc string");
exit(EXIT_FAILURE);
}
strcpy(strings[i], string);
i++;
}
qsort(strings, i, sizeof(*strings), string_cmp);
printf("\nSuccessfully read strings(in sorted order):\n");
for (str = 0; str < i; str++) {
printf("strings[%d] = %s\n", str, strings[str]);
free(strings[str]);
strings[str] = NULL;
}
return 0;
}
I would like to create an array of string variables, and the number of elements is depends on the user's input. For example, if the user's input is 3, then he can input 3 strings. Let's say "aaa", "bbb" and "ccc". They are stored by the same pointer to char(*ptr) but with different index.
code:
int main()
{
int t;
scanf("%d", &t);
getchar();
char *ptr = malloc(t*sizeof(char));
int i;
for(i=0;i<t;i++)
{
gets(*(ptr[i]));
}
for(i=0;i<t;i++)
{
puts(*(ptr[i]));
}
return 0;
}
t is the number of elements, *ptr is the pointer to array. I would like to store "aaa", "bbb" and "ccc" in ptr[0], ptr[1] and ptr[2]. However, errors have been found in gets and puts statement and i am not able to work out a solution. Would someone give a help to me? Thank you!
You shouldn't use gets(), which has unavoidable risk of buffer overrun, deprecated in C99 and deleted from C11.
Only one character can be stored in char. If the maximum length of strings to be inputted is fixed, you can allocate an array whose elements are arrays of char. Otherwise, you should use an array of char*.
Try this (this is for former case):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* the maximum length of strings to be read */
#define STRING_MAX 8
int main(void)
{
int t;
if (scanf("%d", &t) != 1)
{
fputs("read t error\n", stderr);
return 1;
}
getchar();
/* +2 for newline and terminating null-character */
char (*ptr)[STRING_MAX + 2] = malloc(t*sizeof(char[STRING_MAX + 2]));
if (ptr == NULL)
{
perror("malloc");
return 1;
}
int i;
for(i=0;i<t;i++)
{
if (fgets(ptr[i], sizeof(ptr[i]), stdin) == NULL)
{
fprintf(stderr, "read ptr[%d] error\n", i);
return 1;
}
/* remove newline character */
char *lf;
if ((lf = strchr(ptr[i], '\n')) != NULL) *lf = '\0';
}
for(i=0;i<t;i++)
{
puts(ptr[i]);
}
free(ptr);
return 0;
}
You can use this code which is given below because string array is like char 2D array so use can use pointer of pointer and when you allocate memory at run time by malloc then you need to cast into pointer to pointer char type.
int main()
{
int t;
scanf("%d", &t);
char **ptr = (char **)malloc(t*sizeof(char));
int i,j;
for( i=0;i<t;i++)
{
scanf("%s",ptr[i]);
}
for(i=0;i<t;i++)
{
puts(ptr[i]);
}
return 0;
}
Here is an example, of a clean if slightly memory inefficient way to handle this. A more memory efficient solution would use one string of MAX_LINE_LENGTH and copy to strings of precise lengths.. which is why one contiguous block of memory for the strings is a bad idea.
The asserts also just demonstrate where real checks are needed as malloc is allowed to fail in production where asserts do nothing.
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#define MAX_LINE_LENGTH 2048
int
main(void) {
int tot, i;
char **strheads; /* A pointer to the start of the char pointers */
if (scanf("%d\n", &tot) < 1)
return (1);
strheads = malloc(tot * sizeof (char *));
assert(strheads != NULL);
/* now we have our series of n pointers to chars,
but nowhere allocated to put the char strings themselves. */
for (i = 0; i < tot; i++) {
strheads[i] = malloc(sizeof (char *) * MAX_LINE_LENGTH);
assert(strheads[i] != NULL);
/* now we have a place to put the i'th string,
pointed to by pointer strheads[i] */
(void) fgets(strheads[i], MAX_LINE_LENGTH, stdin);
}
(void) printf("back at ya:\n");
for (i = 0; i < tot; i++) {
fputs(strheads[i], stdout);
free(strheads[i]); /* goodbye, i'th string */
}
free(strheads); /* goodbye, char pointers [0...tot] */
return (0);
}