-
Segmentation fault
guys, i'm getting segmentation fault while executing this source code... pls tell me what the problem is.... thanx...
Code:
#include<stdio.h>
int main()
{
int len=0,i=0;
char *str1 = "Protechsoft";
char *str2="Technologies";
while(*(str1+len) != '\0')
len++;
while(*(str2+i)!='\0')
{
*(str1+len)=*(str2+i);
len++;
i++;
}
*(str1+len)='\0';
printf("%s",*str1);
return 0;
}
-
-
thanks for the immediate response
-
in that case if pointer strings are used they can never be modified??
say, for example, as i've been trying to use in my above coding, what kind of string should i use for concatenating with another string??
if i use a char array i wont be able to concatenate since its size will vary... right??
-
You need to use an array, yes. Make sure it's big enough to hold all the data.
The other solution is dynamic memory, but it's more complex and trickier, and might not be for beginners.
-
thanks again..... i shall try and get back to you
-
actually i want to do it using pointers.... is there any way to do so.... i want it to be simple like the incorrect code i tried above..... apologies for bothering u too much
-
Pointers just point to somewhere. You can make a big buffer (array) to hold the data and create a pointer that points to it:
char buf[1024];
char buf2[1024];
char* str1 = buf;
char* str2 = buf2;
-