Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linking strings 1

Status
Not open for further replies.

fitzov

Technical User
Jan 29, 2006
5
US
Help a newbie out with this program:
input--two strings from the user
output--one string with memory allocated.
for example: first and last name.
Here is my code--I'm obviously confused.

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

char add_strings (char *, char *);
char *newstring

main()
{
/*get two strings*/
char a[], b[];
printf ("\nEnter two strings of characters separated by a space: ");
scanf ("%s%s", a[], b[]);

/* allocate memory for the new array */
newstring = malloc (sizeof (newstring) * sizeof(char));

printf ("\nThe new string is %s.", add_strings (a, b));

return 0;
}

/* function add_strings(): concatenates two strings */

char add_strings (char *first, char *second)
{
char newstring = first;

/* some loop to add each element?? */

for
return ();
}
 
You need to specify a size for your char arrays. Example:
Code:
char a[ 80 ], b[ 80 ];
Then when you pass them to scanf(), don't put the [] after a & b.

When you malloc() your new string, you need to add the strlen() of a & b, and add 1 more for the terminating NULL.

add_strings() should return a char* instead of a single char.

Before you return from main(), you should free() the new string that you allocated in add_strings().

To combine the two strings, look up strcpy() and strcat().
 
The following works, but I would like to allocate a specific amount of memory for the appended string.

main()
{
/*get two strings*/
char *a, *b;
printf ("\nEnter two strings of characters separated by a space: ");
scanf ("%s%s", a, b);

a = strcat (a, b);

printf ("\nThe new string is %s.", a);

return 0;
}
 
I'm surprised your program doesn't crash! :eek:
You're declaring two char* pointers, but aren't setting them to any value. So when you pass them to scanf(), they are set to a random pointer in memory which you didn't allocate...

Use an array of chars instead of char*.

Use strlen( a ) and strlen( b ) to get the length of both strings, add them together, and add 1. Then malloc() that much memory for a new char* pointer, and copy a and b into that new string.
 
The following works:

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

int main()
{

char *f_string, *l_string;

/*allocate memory for both strings*/
f_string = (char *)malloc(sizeof (256));
l_string = (char *)malloc(sizeof (128));

/*get two strings*/
printf ("\nEnter your first name: ");
fgets (f_string, 256, stdin);
printf ("\nEnter your last name: ");
fgets (l_string, 128, stdin);

/*concatenate strings --append last to first*/
strcat (f_string, l_string);
printf ("\nYour proper name is %s", f_string);

}

I'm still a bit confused about how the * operator is supposed to work though. Specifically, the relationship between *f_string and f_string. I supppose I could also free the memory used by l_string.
 
> The following works:
Sadly, not as well as you think.

> f_string = (char *)malloc(sizeof (256));
sizeof(256) == sizeof(int) == probably 4 on most machines.

This is a lot less than the 256 you were hoping for.

So it should be
Code:
 f_string = malloc(256);
The cast of malloc is not necessary in C.

--
 
The following works:

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

main()
{

/*allocate memory for two strings*/
char *f_string = (char *)malloc(sizeof(char) * 100);
char *l_string = (char *)malloc(sizeof(char) * 50);

if (f_string == NULL || l_string == NULL)
exit(1);

/*get two strings*/
printf ("\nEnter your first name: ");
gets (f_string);

printf ("\nEnter your last name: ");
gets (l_string);

/*concatenate strings --append space to first and last to that*/

strcat (f_string, " ");

strcat (f_string, l_string);

printf ("\nYour proper name is %s", f_string);

}

The previous comment about casting malloc doesn't make sense given my results when not using it. I will get:

> Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\Borland\BCC55\Bin\Bcc32.exe" "string_mem.c"

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
string_mem.c:
Error E2034 string_mem.c 12: Cannot convert 'void *' to 'char *' in function main()
*** 1 errors in Compile ***
> Execution finished.
 
> The previous comment about casting malloc doesn't make sense given my results when not using it
Because you're using a C++ compiler to compiler a C program.

If you use a C compiler, then that warning will not appear.

> gets (f_string);
It's a backward step to replace fgets() with gets().
Buffer overflow possibilities abound....

--
 
I was planning on learning C++ next, so I guess if I can get along with the C++ compiler I'm that much closer to being finished with this initial training.

You are right about 'gets'. I was aiming for simplicity at that point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top