how do I combine two strings together if two is defined
ouput = "one" + two ???
Printable View
how do I combine two strings together if two is defined
ouput = "one" + two ???
Are you talking about attaching one string to another?
if so, then:
Code:#include <stdio.h>
#include <string.h>
main()
{
char str_output[20]={0}, str_two[]="two";
strcpy(str_output, "one"); // copies "one" into str_output
strcat(str_output, str_two); // attaches str_two to str_output
printf("The Output string is: %s", str_output);
}
/*program to concat without using strcpy and strcat functions */
#include<stdio.h>
#include<string.h>
main()
{char a[]="hello world"; /*ur 1st string goes here*/
char b[]="how are you"; /*sencond string goes here*/
func1(b,a);
getchar();
}
func1(char *b,char *a)
{
int k,j;
k=strlen(a);
for(j=0;j<=strlen(b);j++)
{a[k+1]=b[j];
k++;
}
for(j=0;j<=((strlen(a)));j++)
printf("%c",a[j]);
}
You've been here for 20 posts so far, and you still are too lazy to actually use code tags. You'll want to correct that.
Quzah.
Also, main() is no longer valid C.
Code://program to Combine Two Strings Using Pointers
#include<stdio.h>
#include<conio.h>
main()
{
void combine(char*,char*,char*);
char *a="",*b="Hello",*c="World";
combine(a,b,c);printf("Combined String Is %s",a);
getch();return 0;
}
void combine(char *res,char *str1,char *str2)
{
while(*str1!='\0')
*res++=*str1++;
while(*str2!='\0')
*res++=*str2++;
*res='\0';
}
Your code is wrong, Rose_Flowers. You're accessing memory without allocating it (char *a). It should be char a[100]; or something like that. Or maybe use the new operator to allocate some memory and delete it later. There is no reason at all to not use the strcat() functions as described above.
Wrong forum. C has no 'new' and 'delete' operators. ;)Quote:
Originally posted by Speedy5
Or maybe use the new operator to allocate some memory and delete it later. There is no reason at all to not use the strcat() functions as described above.
Quzah.
Lolzzzzz
it does work when simply you access members
of any array using address operator particularly
in your own functions of void type but if you use
strcpy strcat or such any other function to deal with strings and arrays in your self made function it will certainly not work, in that case you will have to return some
value or to assign it so some variable
I dont know why it is so but it work correctly even if u dont
return any value it will assign returning value to first comment
of that void type function.
if u dont agree with it just compile it with ur TC Compiler and then post ur reply ;) :)
Please actually know what you're talking about here. Granted, I tend to stick my foot in my mouth from time to time, but I usually recognize when I'm wrong.Quote:
Originally posted by Rose_Flowers
Lolzzzzz
it does work when simply you access members
of any array using address operator particularly
You cannot overrun your array boundaries without adverse side effects. Maybe not now, maybe not visible to you, but it is wrong.
Furthermore, you cannot modify string literals.
It is flat out wrong to modify data you have not implicitly allocated. Yes, your compiler will let you make a pointer point to any memory space you want. Yes, you can try to blindly write data to that location. No, it is not correct. It is wrong. It will crash your program, or some one else's program, but it is wrong. End of discussion.
Learn how to correctly access and allocate memory, or don't use pointers.
Quzah.
Sorry I'm kind of highjacking the thread but I've been wondering about this for some time:
say I writeQuote:
Furthermore, you cannot modify string literals.
foo1("Hello World");
and somewhere else in the same module:
foo2("Hello World");
Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?
I cannot see any other reason for this rule, so enlighten me if there is . I realise that you would be writting in the code section of the memory for your process anyway, but after all if you do not write out of bounds...
I think, as is, this rule is bad from a syntactic point of view, it makes no sens, at least to a beginner's understanding :p.
allowing only:
const char* foo = "bar"
and not:
char* foo = "bar"
Would make things much better I think (I'm sure you all impatiently wait for the day I'll get my own C standard out :) ) .
Thanks
Most likely, yes. My understanding of it is that it builds a string table which it stores all string literals in. Any identical string will most likely just be treated as the same entry.Quote:
Originally posted by P.Phant
Sorry I'm kind of highjacking the thread but I've been wondering about this for some time:
say I write
foo1("Hello World");
and somewhere else in the same module:
foo2("Hello World");
Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?
In MSVC++, with no optimizations, here's what I get from the following code example:
When in doubt, try it out.Code:#include <stdio.h>
char * foostr = "Hello World";
int main( void )
{
char *foostr2 = "Hello World";
printf("%p, %p\n", foostr, foostr2 );
return 0;
}
/*
Output:
0042001c, 0042001c
*/
[edit]
I modified printf to use %p instead of %x so it's "more correct" from a learning point of view. :P (Though the end result is really the same.)
So to answer your final question, yes, this really is the reason you can't modify string literals. Because they're optimized into a string table which would basicly break it if you modified one of the "Hello World" strings some place.
[/edit]
Quzah.
Thanks quzah.
:)Quote:
When in doubt, try it out.
Why didn't I think of outputting the pointer? oh well...
It ain't broken, it works exactly as designed ;) And it does care, is just doesn't BSOD ;) ;)Quote:
Originally posted by Salem
> if u dont agree with it just compile it with ur TC Compiler and then post ur reply
Not everyone uses a fossil compiler on a broken OS which lets you spray data all over memory without a care in the world.
Well, not really random -- it points to the exact spot that the "Divide by Zero" interrupt address is...Quote:
Oh, and before you spout about how you just had char *a, remember that you were pointing at a random memory location, which would be randomly used by something else. The fact that your TC compiled program didn't crash was luck.Code://program to Combine Two Strings Using Pointers
#include<stdio.h>
main()
{
void combine(char *, char *, char *);
char *a = NULL, *b = "Hello", *c = "World";
combine(a, b, c);
printf("Combined String Is %s", a);
return 0;
}
It may or may not.Quote:
Originally posted by P.Phant
Will the compiler optimize those two calls to pass the same address, only writting "Hello World" once in the assembly code?
I'm not sure about C90.Quote:
ISO/IEC 9899:1999 (E): 6.5.2.5p8 String literals, and compound literals with const-qualified types, need not designate distinct objects.82)
82) This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations.
And my output for Quzah's code was this.Code:#include <stdio.h>
void foo(const char *s)
{
printf("%p \"%s\"\n", s, s);
}
int main(void)
{
foo("Hello world");
foo("Hello world");
return 0;
}
/* Borland C++ 5.0 for Win32 / Microsoft Windows 2000 [Version 5.00.2195]
0040A131 "Hello world"
0040A13D "Hello world"
*/
Code:00407078, 00407084