CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2013
    Posts
    45

    initialization of a pointer

    I have to write a function,which gets a string,deletes its spaces and returns the result..That's what I did:

    char *removespaces(char *s1)
    {
    char *s2=s1;
    int i,j=0;
    for (i = 0; i<strlen(s1); i++){
    if (s1[i]!=' ') {
    s2[j]=s1[i];
    }else {
    j--;
    }
    j++;
    }
    s2[j]=0;
    return s2;
    }

    Is this right???If yes,could you explain me why I have to initialize the pointer *s2 with the first element of the array s1...???If I don't initialize the pointer,or initialize it with something else,I get a segmentation fault...

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: initialization of a pointer

    This has been answered elsewhere.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    45

    Re: initialization of a pointer

    A ok...Thank you!!!!!

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: initialization of a pointer

    The issue I have with this is that as well as returning the result, it also changes the original string with which I would not be happy. IMO the definition of the function should be

    char *removespaces(const char *s1)
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: initialization of a pointer

    Well, that just a matter of what interface you want, e.g., qsort and std::sort change what they operate on. This could be an advantage if you really want to change the original string since you avoid creating another, and if you don't want, you just make a copy and use that instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured