string copy without arrays

This is a discussion on string copy without arrays within the C Programming forums, part of the General Programming Boards category; Hi I was trying to do the following code reversing a string without using arrays but its giving me segmentation ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    10

    string copy without arrays

    Hi I was trying to do the following code reversing a string without using arrays but its giving me segmentation fault. I hope you guys can help me out.

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

    void main()
    {
    char *s, *c;int i,j;
    printf("enter the string\n");
    scanf("%s", s);
    for(i = strlen(s), j = 0; i >= 0, j <= strlen(s); i--, j++){
    printf("%c\n", *(s+i));
    *(c+j) = *(s + i);
    }
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You aren't allocating any memory for s.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    Thanks a lot for ur help
    But it should copy the reversed string s into C itz not doing that

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    As has been said, s & c doesn't point at any valid memory. You need to allocate some using malloc().
    Also, don't forget about the NULL terminator.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    Yeah I did allocate memory but still the reversed string S is not being copied into the string c.

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

    void main()
    {
    char *s, *c;int i, j;
    s = (char *)malloc(50);
    printf("enter the string\n");
    scanf("%s", s);
    c = (char *)malloc(strlen(s));
    for(i = strlen(s), j = 0; i >= 0; i--){
    *(c + j) = *(s + i);
    j++;
    }
    printf("%s", c);
    free(s); free(c);
    printf("\n");
    }
    </code>

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First, to be able to hold the NULL terminator the size of c must be strlen(s) + 1.

    Second, to prevent the first element from being the NULL terminator, start looping at strlen(s) - 1.

    Third, you never add a NULL terminator at the end of the array, meaning the string will never end.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    More about NULL.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Fourth, your code tags are wrong, proving that you either didn't read, or didn't pay attention to, the thread for it.

    [code]
    ...your code here...
    [/code]

    Fifth, you're using void main, which shows that you are either an idiot, have an idiot for a teacher, or you haven't read the FAQ/countless discussions on it...
    Code:
    int main ( ...pick your poison... )
    {
        ...do your damage...
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    But strings automatically terminate with a NULL character

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    Thanx a lot guyz it did work. thanx a lot again

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by kate1234
    But strings automatically terminate with a NULL character
    Yes. By definition, a string is a series of one or more characters, terminated by a null. So yes, all strings have a null. However, a given sequence of characters does a string make.
    Code:
    char *mystring = "This has a null automaticly.";
    char myarray[] = "This has a null automaticly also.";
    char my2ndarray[5] = { 'H', 'e', 'l', 'l', 'o' };
    The final example does not have a null automaticly. You can act like it's a string, and C will not prevent you from doing so:
    Code:
    printf( "%s", my2ndarray );
    This will actually print 'Hello', however, you'll get a bunch of crap on the end because it's not null terminated. (IE: String based functions such as this will try and continue to display or use all characters (ie: bytes of memory) until they finally reach a null.)

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    Thanx a lot quzah for making the concepts clear

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Sixth, this thread is funny

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 06:27 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Replies: 3
    Last Post: 11-03-2002, 01:14 AM

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