So I got this assignment to combine two arrays without using strcat(). The two arrays must be passed to function as parameters. The arrays need to be merged into one array in the function with dynamic memory allocation and then the new string needs to be returned back to main.

Here's what I have coded so far, but it still prints nothing or gives warning:
d21.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'

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

char combinearrays(const char *array1, const char *array2);

int main () {

  int i;
  const char *array1[50] = {"asgfhararjsrj"};
  const char *array2[50] = {"wajwarjwrjarjsdafj"};
  char cjj[100];


cjj[100]=combinearrays(*array1, *array2);

for (i=0; i!='\0'; i++){
    printf("%s", cjj[i]);
}
putchar('\n');

return 0;
}

char combinearrays(const char *array1, const char *array2) {
int i;
int j=0;
char *text;

text = malloc(( strlen( array1 ) + strlen( array2 ) + 2 )* sizeof(char));
    if (!text) {
        printf ("Not enough memory");
        exit(1);
        }

for (i=0; i < (strlen( array1 )); i++) {
     text[i] = array1[i];
}

for (j=strlen(array1); j < (strlen( array1 ) + strlen( array2 ) ); j++) {
     text[j] = array2[j];
}
   text[j+1]='\0';
   return *text;
   free (text);
   
}

This just seems to be impossible for me to solve alone.

>>char combinearrays(const char *array1, const char *array2) {

The function needs to return a pointer to an array, not a single character.

>>return *text;
>>free (text);

line 45 -- free(text) -- is unreachable and will never get executed because of line 44.

line 44: should be return text; -- remove the star

This is what I got after removing the star.

d21.c: In function 'main':
d21.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
d21.c: In function 'combinearrays':
d21.c:44: warning: return makes integer from pointer without a cast

line 15: >>cjj[100]=combinearrays(*array1, *array2);

cjj should be declared as a char*, not an array. And remve the start from that function call/

char* cjj;
cjj = combinearrays(array1, array2);

line 40: that loop will not work. You need to use both i and j counters.

for(j = 0; j< strlen(array2) j++, i++)
   text[i] = array2[j];

Edited 6 Years Ago by Ancient Dragon: n/a

Still gives me the same warning reports. Reports about imcompatible pointer types now too. This is just so confusing. :s

d21.c: In function 'main':
d21.c:15: warning: passing argument 1 of 'combinearrays' from incompatible pointer type
d21.c:5: note: expected 'const char *' but argument is of type 'const char **'
d21.c:15: warning: passing argument 2 of 'combinearrays' from incompatible pointer type
d21.c:5: note: expected 'const char *' but argument is of type 'const char **'
d21.c:15: warning: assignment makes pointer from integer without a cast
d21.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
d21.c: In function 'combinearrays':
d21.c:44: warning: return makes integer from pointer without a cast

The two arrays in main() are declared wrong

int main () {

  const char array1[50] = {"asgfhararjsrj"};
  const char array2[50] = {"wajwarjwrjarjsdafj"};
  char *cjj;


cjj=combinearrays(array1, array2);
printf("%s\n", cjj);

return 0;
}

d21.c: In function 'main':
d21.c:15: warning: assignment makes pointer from integer without a cast
d21.c:18: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
d21.c: In function 'combinearrays':
d21.c:44: warning: return makes integer from pointer without a cast

I tried some other changes too after that, but just got a lot of more errors.

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

char combinearrays(const char *array1, const char *array2);

int main () {

  int i;
  const char array1[50] = {"asgfhararjsrj"};
  const char array2[50] = {"wajwarjwrjarjsdafj"};
  char* cjj;


cjj=combinearrays(array1, array2);

for (i=0; i!='\0'; i++){
    printf("%s", cjj[i]);
}
putchar('\n');

return 0;
}

char combinearrays(const char *array1, const char *array2) {
int i;
int j=0;
char *text;

text = malloc(( strlen( array1 ) + strlen( array2 ) + 2 )* sizeof(char));
    if (!text) {
            printf ("Not enough memory");
            exit(1);
        }

for (i=0; i < (strlen( array1 )); i++) {
     text[i] = array1[i];
}

for (j=strlen(array1); j < (strlen( array1 ) + strlen( array2 ) ); j++) {
     text[j] = array2[j];
}
   text[j+1]='\0';
   return text;
   free (text);
   
}

This is the new code after the modifications that ancient dragon told me.

The issues:
1. malloc should have been done in the main() function, so that free is possible in the main() function.
2. you are assigning the memory which is already free (undefined behaviour); dangling pointer issue.
3. the declaration and definition of
char combinearrays(const char *array1, const char *array2);
should have been
char *combinearrays(const char *array1, const char *array2);
4. if you want to implement strcat() like function the, why not store the concatinated string infromation in array1[], of course you should be sure that there is enough memory in array1[], to hold the information.

I overlooked one thing you are trying to free the memory after the return statement, which is impossible.
It should have at least given a warning.

you are trying to free memory after return statement, which is impossible. I hope there should have been a warning regarding this.

1. This is school assignment and I thought when I readed it that malloc needs to be done in the function. I'm not too familiar with using it yet.

4. The assignment says that I need to calculate enough space with malloc for a new array that has array1 and array2 combined in it.

Thanks for your advices. Maybe I can try something with strcopy() function.

Combinearrays function can only have those two strings as it's parameters so this assingment has some frustrating boundaries.

>>1. malloc should have been done in the main() function, so that free is possible in the main() function.

That isn't necessary. There are several functions in the standard C libraries which allocate memory and are free'ed by the calling application program.

>>so this assingment has some frustrating boundaries
That's because you have failed to read and comprehend what others have told you about your program. Go back to the beginning and re-read our comments, then compare them with your program. You will then see your mistakes. For example I mentioned the problem with free() in your program over an hour ago, in my first post in this thread. Yet you have failed to change it. What's the point of us trying to help you if you just ignore us.

Edited 6 Years Ago by Ancient Dragon: n/a

I posted my code an hour ago too so how do you know what I have changed and what not.
I thought I had changed the code just as you said. I already started with making new main with malloc function in it because super-sonic told it's impossible for it to be in function.

Ofcourse I haven't ignored your posts. It just ain't so easy for me to understand C yet as I have studied it just for one month.

Now I'm gonna go get some sleep. Will get back to programming and thinking about your posts tomorrow.

I posted my code an hour ago too so how do you know what I have changed and what not.

Then why are you asking for more help if you aren't telling us the current state of the program? Aren't you asking us to shoot a moving target -- while blindfolded?

Listen to the Dragon. He's trying to help using the definition of the problem as you stated it. super-sonic is changing the definition of the problem because he does not understand or didn't read your requirements.

>>Now I'm gonna go get some sleep. Will get back to programming and thinking about your posts tomorrow.

Good. Better to start again with a fresh mind :)

Moved free() to main and changed the code

for (j=0; (j < strlen( array2 )); j++, i++) {
     text[i] = array2[j];
}

Full code atm is:

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

char *combinearrays(char *array1, char *array2);

int main () {

  int i;
  char array1[50] = {"asgfhararjsrj"};
  char array2[50] = {"wajwarjwrjarjsdafj"};
  char* cjj;


cjj=combinearrays(array1, array2);

for (i=0; i!='\0'; i++){
    printf("%c", cjj[i]);
}
putchar('\n');
free (cjj);
cjj = NULL;
return 0;
}

char *combinearrays(char *array1, char *array2) {
int i;
int j;
char *text;

text = malloc(( strlen( array1 ) + strlen( array2 ) + 2 )* sizeof(char));
    if (!text) {
            printf ("Not enough memory");
            exit(1);
        }

for (i=0; i < (strlen( array1 )); i++) {
     text[i] = array1[i];
}

for (j=0; (j < strlen( array2 )); j++, i++) {
     text[i] = array2[j];
}
   return text;

   
}

I just can't figure out my mistakes. It doesn't give any warnings or errors, but when I run it it doesn't do anything.

> gcc -ansi -Wall -pedantic d21.c
> a.out

>

Couldn't edit my code or post earlier as my network connection was down and I couldn't even access gcc compiler via ssh secure shell client.

Ok I got the code working! The problem was in the lines

for (i=0; i!='\0'; i++){
    printf("%c", cjj[i]);
}

I just changed it to printf("%s", cjj); and now the program works.
Thanks for all the help.

Actually it took me about 10 minutes to get the program working after I readed your comments again today 4 hours ago. Like I said; couldn't compile it earlier as my network was down. Why do you still need to post with such an arrogant attitude?

// simple merge two string without string function.
simple merge two string without string function.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
//variable declaration.
char first_string[20];
char second_string[20];
char merge_2_string[40];
int i,j;
clrscr();
cout<<"Enter first string = ";
cin>>first_string;
cout<<"Enter second string = ";
cin>>second_string;
for(i=0;first_string!='\0';i++)
{
merge_2_string= first_string;
}
for(j=0;second_string[j]!='\0';j++)
{
merge_2_string[i+j]= second_string[j];
}
merge_2_string[i+j]='\0';
cout<<"\n";
cout<<"their are Merge by given string= "<<merge_2_string;
return 0;
}

This question has already been answered. Start a new discussion instead.