-2

I am trying to merge multiple string array tokens into one, for example:

char* args = { "zero" , "one', "two"}

Noting that args is not a set size, it could be 1 or 100

I want to make a new array

char* array ={"one two"}

with the spaces in between.

I tried this:

char* array; 
int i=1;
    for(i=1; i< sizeof(args)-1 ; i++){
        strcat(array, args[i]);  
    }

but kept getting a segmentation fault.

This is the code I am using, need to add function that combines all of my args from 1 (not zero since arg[0] has a command) to whatever size:

else if ( (args[1] != NULL) && (strchr(args[1], '=') != NULL) ) {
 

    memcpy(aliasCommands[aliasCount], args[1], 512 ); 

    printf("Save Alias\n");

    printf("Alias Added : %s\n", aliasCommands[aliasCount]);

    aliasCount++;

    printf("Alias Number : %d\n", aliasCount);

        }

Instead of only args[1], I want to pass args[1] space args[2] and so on

Lets say that

args[1]={"command='ls"}
args[2]={"cd'"}

and so on, args could be up to 512. I want to pass them all as one string in

aliasCommand[aliasCount] = {"command='ls cd'"}
SirHammouD
  • 31
  • 7
  • The method you tried would work except you never allocated `char* array`... – Irelia Apr 18 '21 at 19:20
  • Allocate enough memory to hold every token as well as the space between each token as well as a null terminator, then you can try strcat . – Irelia Apr 18 '21 at 19:21
  • Does this answer your question? [Concatenate 3 strings and return a pointer to the new string C](https://stackoverflow.com/questions/34053859/concatenate-3-strings-and-return-a-pointer-to-the-new-string-c) – Irelia Apr 18 '21 at 19:26
  • Your code does not contain any strings. If you use single quotes, you create characer literals if type `int`. If you enclude multiply characters in single quotes, you get a multi-byte character literal with value that is implementation defined. What you want is using double quotes `"` instead of `'`. – Gerhardh Apr 18 '21 at 19:40
  • I updated my code to make it more clear, that doesn't work for me. – SirHammouD Apr 18 '21 at 22:16

2 Answers2

0

You have several problems

  1. You don't calculate the number of elements properly

  2. You don't have room to store the result

  3. strcat wants a nul terminated string

I will use snprintf instead of strcat in order to avoid buffer overflows, something like:

#include <stdio.h>

int main(void)
{
    char *args[] = {"zero", "one", "two"};
    char str[32];
    size_t len = 0;

    for (size_t i = 0; i < sizeof args / sizeof *args; i++)
    {
        len += (size_t)snprintf(str + len, (sizeof str) - len, "%s ", args[i]);
    }
    // Remove the last space
    str[len - 1] = '\0';
    printf("<%s>\n", str);
    return 0;
}

Output:

<zero one two>
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

For starters the initializers used in this declaration (where you forgot to place a semicolon)

char* args[] = { 'zero' , 'one', 'two'};

are not string literals but multibyte integer character constants that have implementation defined values.

It seems you mean

char * args[] = { "zero" , "one", "two" };

Also this declaration of an array (where you again are using an integer character constant instead of a string literal)

char* array[]={'zero one two'}

does not make a great sense because it is equivalent to

char* array[1]={'zero one two'}

It seems you mean

char *array={ "zero one two" }

What you need is to allocate dynamically an enough large array and copy the string literal pointed to in the array args in the array array.

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void )
{
    char * args[] = { "zero" , "one", "two" };
    const size_t N = sizeof( args ) / sizeof( *args );

    size_t n = 0;
    for (size_t i = 0; i < N; i++) n += strlen( args[i] );

    char *array = malloc( n + N );

    if (array != NULL)
    {
        size_t n = 0;
        array[0] = '\0';
        for (size_t i = 0; i < N; i++)
        {
            if (i != 0)
            {
                array[n] = ' ';
                array[++n] = '\0';
            }

            strcpy( array + n, args[i] );
            n += strlen( args[i] );
        }
        puts( array );
    }

    free( array );
}

The program output is

zero one two
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335