Thread: Question about initialization of a pointer

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

    Question about initialization of a pointer

    Hi!!!Here is a function,which deletes the spaces of a string...

    char *removespaces
    (char *s1)
    {
    Code:
      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;
    }
    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...
    Last edited by evinda; 04-27-2013 at 12:11 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You haven't allocated any memory for s2 to use, so your s2[j] is a real problem. I suggest you click on the C tutorial link at the top of the web site, and review the part that deals with memory allocation, especially malloc() .

    You don't HAVE to use that pointer, btw. It's OK to use another static array or just j for this.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I don't know why s2 is even used, since it's set to the same as s1.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I havent get taught malloc() yet...Isn't it right to use pointers???

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I have to write a function,that removes the spaces of a string,and to return the result,so I thought that I should use an other pointer...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by evinda View Post
    I havent get taught malloc() yet...Isn't it right to use pointers???
    A pointer needs some memory to point to, that has been set aside. You don't have that.

    I pointer is like the tip of a compass needle - it points to something - maybe a bucket - that will hold water. The compass needle itself, can not hold water - that's what the bucket's for.

    No you shouldn't be using a pointer, yet. You should be using the variables i and j. You are almost there!

    Take half a dozen pennies and put them in a line, on the table. Put some spaces between some of the pennies.

    Now slowly adjust the pennies going from one end to the other, so the spaces are removed.

    How did you do that? Describe it in step by step simple terms that the computer can work with. Repeat that a few times, and you'll see an insight into the logic you need for this function.

    Yes, I know exactly what your assignment is. It's very common.
    Last edited by Adak; 04-27-2013 at 01:01 PM.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    But....the exercise requires the use of pointers....

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by evinda View Post
    But....the exercise requires the use of pointers....
    Instead of using the indices i and j, you could use two pointers. They both start at the address of s1, and then diverge from one another, as spaces are found and removed, from s1.

    There is no good reason to return a char pointer, since s1 is already being returned modified, with no more spaces.

    What else would you return, besides s1?

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I don't know... what else could I return?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by evinda View Post
    I don't know... what else could I return?
    You have s1, but the size of s1's string will have changed. I would return that new length of the string s1.

    But that would be an int, not a char pointer.

    You could return a char pointer with the address of the last char in the string. Then subtract s1 from it, and use that as the new length of the s1 string.

    That would make sense. Not my preference, but sense.
    Last edited by Adak; 04-27-2013 at 11:47 PM.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Returning the original pointer allows you to include your function as part of a statement, for example:

    Code:
        printf("%s\n", removespaces("this is a test"));
    will print "thisisatest".

    In your original code example, s2 isn't needed and every occurrence of s2 in your program could be replaced with s1 and it would still work.

    As Adak mentioned, if you were supposed to just use pointers without indexing, then you would need s2, and you'd also need another pointer to preserve the original input pointer. Here's a partial example of this method, but missing the part you're probably supposed to figure out as part of your assignment:

    Code:
    char *removespaces(char *s0)
    {
    char *s1 = s0;
    char *s2 = s0;
    
    
        while(*s1){
            if(' ' != *s1)
    
    
    /*  you fill in this part */
    
    
        }
        *s2++ = 0;
    
    
        return s0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Pointer Initialization
    By johan.g1 in forum C Programming
    Replies: 6
    Last Post: 11-14-2012, 09:08 PM
  3. Struct pointer initialization
    By pe4enka in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2010, 05:16 AM
  4. Pointer Initialization Within A Struct
    By SMurf in forum C Programming
    Replies: 15
    Last Post: 02-09-2009, 11:27 AM
  5. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM

Tags for this Thread