character pointers

Thread Starter

sall

Joined Jul 13, 2011
10
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++
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Thread Starter

sall

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

Rich (BB code):
#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;}
 

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
#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;
}
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:
Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
    char s = "hello\n";
    char* p = &s
    char* m[sizeof(s)];        // make a buffer that will fit string
    int i;
    int j = 0;
    for (i=0;i<5;i++)
    {
        if (*(p+i) != 'l')
        {
            *(m+j)=*(p+i);
            j++;    // only increment j to next char 
                    // when we copy a character
                    // so it is pointing to next free space
        }
    }
    (m+j) = 0;    // trailing null
    puts(m);
    system("pause");   // is this really necessary?
    return 0;
}
 

joeyd999

Joined Jun 6, 2011
5,283
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:

Rich (BB code):
#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;
}
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:
Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
    char s = "hello\n";
    char* p = &s
    char* m[sizeof(s)];        // make a buffer that will fit string
    int i;
    int j = 0;
    for (i=0;i<5;i++)
    {
        if (*(p+i) != 'l')
        {
            *(m+j)=*(p+i);
            j++;    // only increment j to next char 
                    // when we copy a character
                    // so it is pointing to next free space
        }
    }
    (m+j) = 0;    // trailing null
    puts(m);
    system("pause");   // is this really necessary?
    return 0;
}
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
    char* p = "hello\n";
    char* m = ".....\n";        // make a buffer that will fit string
    int i;
    int j = 0;
....
}
 

debjit625

Joined Apr 17, 2010
790
@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.

Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
    char s = "hello\n";
    char* p = &s
    char* m[sizeof(s)];        // make a buffer that will fit string
    int i;
    int j = 0;
    for (i=0;i<5;i++)
    {
        if (*(p+i) != 'l')
        {
            *(m+j)=*(p+i);
            j++;    // only increment j to next char 
                    // when we copy a character
                    // so it is pointing to next free space
        }
    }
    (m+j) = 0;    // trailing null
    puts(m);
    system("pause");   // is this really necessary?
    return 0;
}
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
Rich (BB code):
char * s = "hello";
rather
char s = "hello\n";
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
Rich (BB code):
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
Rich (BB code):
char* p = &s
It should be like this
Rich (BB code):
    char* p = s;
Anyway you dont need to use *p variable...

Always initialize your variables
Rich (BB code):
Rich (BB code):
int i = 0;
int j = 0;
In loop don't hard code the size of the string use sizeof operator.
Rich (BB code):
for(i=0;i<sizeof(s);i++)

Last,
Rich (BB code):
(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
Rich (BB code):
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
Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
int main ()
{ 
char s[] = "hello";
char m[sizeof(s)]; // make a buffer to hold string
int i = 0;
int j = 0;
for (i=0;i<sizeof(s);i++)
{
if (*(s+i) != 'l')
{
*(m+j)=*(s+i);
j++; // only increment j to next char 
// when we copy a character
// so it is pointing to next free space
}
}
m[j] = 0; // trailing null
puts(m);
system("pause");
return 0;
}
Good Luck
 

Thread Starter

sall

Joined Jul 13, 2011
10
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
 

Thread Starter

sall

Joined Jul 13, 2011
10
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
 

joeyd999

Joined Jun 6, 2011
5,283
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
Rich (BB code):
char s[] = "hello";
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:

debjit625

Joined Apr 17, 2010
790
joeyd999 said:
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).
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
Rich (BB code):
char str[] = "Debjit";
int size = sizeof(str)/sizeof(str[0]);
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.
 

THE_RB

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

Rich (BB code):
#include <stdio.h>
#include <iostream>
int main ()
{ 
    char* p = "hello";     // compiler inserts NULL
    char m[10];            // is long enough to hold any data you need
    int i = 0;
    int j = 0;
    while(p != 0)       // break on input string NULL found
    {
        if (p != 'l')   // if input not 'l'
        {
            m[j] = p;   // copy to output
            j++;
        }
        i++;
    }
    m[j] = 0;              // must insert NULL at output string end
    puts(m);
    system("pause");
    return 0;
}
 
Top