Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; guys, i'm getting segmentation fault while executing this source code... pls tell me what the problem is.... thanx... Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    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;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    String literals should be const char*.
    That is why you have the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks for the immediate response

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    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??

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks again..... i shall try and get back to you

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    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

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks a lot !!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21