character pointers

Discussion in 'Programmer's Corner' started by sall, Jul 13, 2011.

  1. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    whenever i compile the following code (c language
    ) it causes my console window to stop working.
    #include <stdio.h>
    #include <iostream>
    int main ()
    { char*p="hello";
    char*m;int i;
    for (i=0;i<5;i++)
    {if (*(p+i) != 'l')
    {*(m+i)=*(p+i);}}
    puts(m);
    system("pause");
    return 0;}
    i am suppose to omit the alphabet 'l' from "hello" and print the string without any holes using pointers.so i wrote the above code.compiler is dev c++
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Is this homework? If so, let me know and I will move it to the homework section.

    hgmjr
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Other then the "system("pause");" just before the return that may be hanging your system I can't tell what this mess is doing as YOU DID NOT USE CODE TAGS.
     
  4. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    Perhaps now i used the code tags.

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <iostream>
    3. int main ()
    4. { char*p="hello";
    5. char*m;int i;
    6. for (i=0;i<5;i++)
    7. {if (*(p+i) != 'l')
    8. {*(m+i)=*(p+i);}}
    9. puts(m);
    10. system("pause");
    11. return 0;}
     
  5. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    No sir this isnt homework
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    OK, even with tags it's still a mess.

    Lesson 1: ALWAYS USE WHITESPACE, you know, spaces and carriage returns to make the code flow cleanly:

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <iostream>
    3. int main ()
    4. {
    5.     char* p = "hello";
    6.     char* m;
    7.     int i;
    8.     for (i=0;i<5;i++)
    9.     {
    10.         if (*(p+i) != 'l')
    11.         {
    12.             *(m+i)=*(p+i);
    13.         }
    14.     }
    15.     puts(m);
    16.     system("pause");
    17.     return 0;
    18. }
    There's a bunch wrong here:

    1: p points to a non-null terminated string. You need a zero at then end for C/C++ to recognize the end of a string.

    2: m is not initialized to anything. It points to something that is a char in size, but what it points to has not been set.

    3: "*(m+i)=*(p+i);" says "put the character pointed to by (p+i) to (m+i)," but even if p pointed to something you just skip over the "l" in both cases, so when the character is "l" you just don't copy, but next time you copy to the next character.

    So m would point to a string like so: "he??o" where the ? means you just didn't copy the character.

    Since m is uninitialized I suspect this is causing a crash on the first time thru the loop.

    4: puts(m); is going to attempt to use this uninitialized pointer to a non-zero terminated string and print it. It will continue printing until it either finds a zero somewhere, or accesses protected memory and again causes a crash.

    Untested code follows:
    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <iostream>
    3. int main ()
    4. {
    5.     char s = "hello\n";
    6.     char* p = &s
    7.     char* m[sizeof(s)];        // make a buffer that will fit string
    8.     int i;
    9.     int j = 0;
    10.     for (i=0;i<5;i++)
    11.     {
    12.         if (*(p+i) != 'l')
    13.         {
    14.             *(m+j)=*(p+i);
    15.             j++;    // only increment j to next char
    16.                     // when we copy a character
    17.                     // so it is pointing to next free space
    18.         }
    19.     }
    20.     (m+j) = 0;    // trailing null
    21.     puts(m);
    22.     system("pause");   // is this really necessary?
    23.     return 0;
    24. }
     
  7. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,695
    I may be badly mistaken here, but since s is a pointer to a character string, sizeof(s) should return the size of the pointer variable, not the size of the string. I think a call to strlen() is in order here.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You may well be correct. Sometimes I try to get too fancy w/o my copy of K&R around.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Oops, that aint gonna work either, strlen is a runtime thing, and it needs the length at compile time (unless he mallocs as in his other thread).

    So just hard code the second buffer like the first:

    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2. #include <iostream>
    3. int main ()
    4. {
    5.     char* p = "hello\n";
    6.     char* m = ".....\n";        // make a buffer that will fit string
    7.     int i;
    8.     int j = 0;
    9. ....
    10. }
     
  10. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,695
    Yes. When I mentioned strlen(), I intended that the destination string would be malloc'ed in runtime.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    'K, I thought you'd get it, wasn't so sure about the OP. ;)
     
  12. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    @sall your code is full of errors and the logic of doing the task is also not correct,some of them is eliminated by ErnieM but still in his code their are errors I will come to them one by one I will be using ErnieM's code as the final program as his logic is correct.

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <iostream>
    4. int main ()
    5. {
    6.     char s = "hello\n";
    7.     char* p = &s
    8.     char* m[sizeof(s)];        // make a buffer that will fit string
    9.     int i;
    10.     int j = 0;
    11.     for (i=0;i<5;i++)
    12.     {
    13.         if (*(p+i) != 'l')
    14.         {
    15.             *(m+j)=*(p+i);
    16.             j++;    // only increment j to next char
    17.                     // when we copy a character
    18.                     // so it is pointing to next free space
    19.         }
    20.     }
    21.     (m+j) = 0;    // trailing null
    22.     puts(m);
    23.     system("pause");   // is this really necessary?
    24.     return 0;
    25. }
    26.  
    First of all the header that has been included <iostream> that shouldn't be included as its from C++ standard library and its a C program,here you will need <stdlib.h> header file as the program is using the function "system("pause")"

    Secondly declaring a constant C string is done like this
    Code ( (Unknown Language)):
    1. char * s = "hello";
    rather
    which is declaring a char rather a const C string ,this will give error.
    You don't need to add the special char i.e.. new line "\n" in the C string as when you use "puts" function it include a new line after the string.

    Here joeyd999 said about the sizeof stuff thats true,if you really want to use sizeof operator to get the size of the string declare the string like this
    Code ( (Unknown Language)):
    1. char s[] = "hello";
    Don't get confused,C string is nothing more than a char array...

    Third,you are assigning an address of a char pointer to a char pointer
    Code ( (Unknown Language)):
    1. char* p = &s
    It should be like this
    Code ( (Unknown Language)):
    1.     char* p = s;
    Anyway you dont need to use *p variable...

    Always initialize your variables
    Code ( (Unknown Language)):
    1.  
    Code ( (Unknown Language)):
    1.  
    2. int i = 0;
    3. int j = 0;
    4.  
    In loop don't hard code the size of the string use sizeof operator.
    Code ( (Unknown Language)):
    1. for(i=0;i<sizeof(s);i++)


    Last,
    Code ( (Unknown Language)):
    1. (m+j) = 0;
    you can't do this as the code "m+j" will create an address and you can't assign a value to an address.
    it should be like this
    Code ( (Unknown Language)):
    1. m[j] = 0;
    Their are better ways to do this but I will go with ErnieM's code as he has already built the logic.

    Here is the final code
    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. int main ()
    5. {
    6. char s[] = "hello";
    7. char m[sizeof(s)]; // make a buffer to hold string
    8. int i = 0;
    9. int j = 0;
    10. for (i=0;i<sizeof(s);i++)
    11. {
    12. if (*(s+i) != 'l')
    13. {
    14. *(m+j)=*(s+i);
    15. j++; // only increment j to next char
    16. // when we copy a character
    17. // so it is pointing to next free space
    18. }
    19. }
    20. m[j] = 0; // trailing null
    21. puts(m);
    22. system("pause");
    23. return 0;
    24. }
    25.  
    Good Luck
     
  13. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    thanks for the reply i will use the white space.Also, about the first point you made the compiler automatically inserts null at the end of the string.i have made programs to check it and they all print the string correctly
     
  14. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    thanks sir your code was right.i had one doubt that you terminated the m string with null so u wrote this separately guess there is no need for that as it is already done in the loop.i ran the code without that command (m[j] = 0) it ran perfectly. actually teacher told us to use only pointers so i was not sure whether i could use an array in the program.
    thx
     
  15. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,695
    Interesting. I still would have thought that sizeof(s) should return the size of the pointer, not the size of the string. I tested this on gcc, and, lo and behold, it really gives the size of the string. Now I gotta go figure this out!

    Edit: I just confirmed this. BTW, it returns the length of the array, including the ending \0 byte (in the case of a string).
     
    Last edited: Jul 14, 2011
  16. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Because inside the loop we copied the last value of the array i.e.. 0.
     
  17. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Its actually,not the way you should do but in this case we can.The sizeof operator returns the size of the array in bytes rather the size of the string.So the C string is of ASCII char (Normally we all use that as the chat type) which is one byte in size,so the sizeof the array is all the char plus the null termination.It works but if we use unicode char set which use 2 bytes per char then the result would be wrong for that we also have solution
    Code ( (Unknown Language)):
    1.  
    2. char str[] = "Debjit";
    3. int size = sizeof(str)/sizeof(str[0]);
    4.  
    Here I am assuming that your compiler is compiling the program with unicode char set which use 2 bytes for a char data type.The total size of "str" would be 14 bytes including null termination char now a singhe char will size "str[0]" 2 bytes so the size of the string becomes 7 with including the null termination.
     
  18. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    That's such an ugly use of pointers. This type of referencing below has a LOT of safety benefits for simple string tasks;

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <iostream>
    4. int main ()
    5. {
    6.     char* p = "hello";     // compiler inserts NULL
    7.     char m[10];            // is long enough to hold any data you need
    8.     int i = 0;
    9.     int j = 0;
    10.     while(p[i] != 0)       // break on input string NULL found
    11.     {
    12.         if (p[i] != 'l')   // if input not 'l'
    13.         {
    14.             m[j] = p[i];   // copy to output
    15.             j++;
    16.         }
    17.         i++;
    18.     }
    19.     m[j] = 0;              // must insert NULL at output string end
    20.     puts(m);
    21.     system("pause");
    22.     return 0;
    23. }
    24. [/i][/i][/i]
     
  19. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    I don't think so and what are the safety benefits you are talking about?
     
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Well it has the distinct benefit of hijacking a thread on pointers to talk about arrays.
     
Loading...