character pointers

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

Thread Status:
Not open for further replies.
  1. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    I wrote the following code (c language) to omit the alphabet 'l' from string "hello" and print the resultant string without holes using pointers.it printed garbage values then caused the compiler to stop working.
    #include <stdio.h>
    #include <iostream>
    int main ()
    { char*p="hello";
    int i;
    (p) =(char*)malloc(20);/*assigning memory of 20 bytes*/
    for (i=0;i<5;i++)
    {if (*(p+i) = 'l')
    {
    free(p+i);}}/*i tried to free the specific memory containing alphabet l*/
    puts(p);
    system("pause");/*to pause the console window*/
    return 0;}

    can someone tell what mistake is there with this code or the correct code to omit the alphabet 'l' from string "hello" and print the resultant string using pointers ?
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I have locked this thread to minimize confusion since it is a duplicate.

    hgmjr
     
Loading...
Thread Status:
Not open for further replies.