Question:
pointer in C programming. i have question.?
Paul Siga
2011-12-22 08:27:07 UTC
void main()
{
char *str,*ptr;
int wc=0;
clrscr();
printf("\n enter a sentence");
gets(str);
for(ptr=str;*ptr ! ='\0'; ptr++)
{
if(*ptr==' ')
{
wc++;
}
}
wc++;
printf("\n no of words=%d",wc);
getch();
}

[PLEASE EXPLAIN THIS FOR LOOP LIKE BEGINNER - for(ptr=str;*ptr!='\0'; ptr++)
Three answers:
?
2011-12-22 08:52:29 UTC
int main()

{

char *ptr; // str and ptr are defined to be pointers to char

char str[100];// allocate space for the str

int wc=0;

printf("\n enter a sentence");

gets(str); // unsafe way to enter a string -- no bounds checking

for(ptr=str;*ptr ! ='\0'; ptr++)

// give ptr the starting address of str

// *ptr !='\0' is the hard way of saying *ptr !=0

// better is to say *ptr; since you want to stop when ptr points to a zero byte

// since zero is false just saying *ptr is enough

// for(ptr=str; *ptr; ptr++)

// ptr++ means to add one to ptr so it points to the next byte

{

if(*ptr==' ') // check if ptr points to a space

{

wc++; // if so add one to wc

}

}

wc++; // add one again

printf("\n no of words=%d",wc); // not really number of words since there could be two spaces twixt words.

getch();

return 0;

}



there were many problems with your programme:

main returns an int

str was not assigned anywhere to point -- it has an undefined value so the gets function will put your input at a ramdom place in memory leading to memory corruption

wc counts spaces NOT words it will be off if there are tabs or more than one space between words or spaces after the text.
oops
2011-12-22 16:55:17 UTC
Sure. But before I do that. There are some far more important things that you need to learn.



First, when you declare a pointer without assigning it to memory that has been properly allocated, either statically or dynamically, it is undefined behavior to dereference it. And you definitely should not be trying to write to the address it points to, as you do when you pass str to the gets function.



Second, never use gets. It doesn't allow you to set any kind of limit on how many characters will be read. So the user can just type and type to his hearts content, and gets will store it all, even if there is not enough memory allocated in the specified buffer. Use fgets instead.



Third, the return value of main is an int. Your compiler may accept void, but it is not proper C.



On to your question. For the time being, we will assume the rules above had not been grossly violated, and that str points to a properly allocated buffer in memory.



    ptr=str;



That assigns ptr to point to the same place as str, which is the first character of the sentence that the user entered.



    *ptr!='\0';



In C, strings are zero terminated. When we want to pass strings around, we just pass a pointer to the beginning of them. This pointer does not contain information about the length of the string. We find the end by incrementing forward until we get to a zero. The above statement is checking for that zero.



    ptr++



Pretty basic. This advances the pointer to point to the next character.



if (*ptr==' ')



This is checking if the character pointed to is a space.



All together, what that loop does is scan through the sentence, one character at a time, counting the number of spaces. The intent being to use that to figure out the number of words. The assumption of course being that there is precisely one space between each word, therefore the number of spaces is one less than the number of words.



Here's what the code should look like if you don't want to violate the rules I gave at the beginning:



    int main()

    {

        const int MAX_SENTENCE = 200;

        char str[MAX_SENTENCE + 1];

        char *ptr;



        int wc=0;

        clrscr()

        printf("\n enter a sentence");

        fgets(str, MAX_SENTENCE, stdin);



        for(ptr=str; *ptr !='\0'; ptr++)

        {

            if(*ptr==' ')

            {

                wc++;

            }

        }

        wc++;

        printf("\n no of words=%d",wc);

        getch();



        return 0;

    }
anonymous
2011-12-22 16:51:30 UTC
The characters in the string are given to the pointer first.Each character is stored in a specific address at the end null character is given('/0') then each address of the character is checked for the null character if it is present(that is the end of the string length) if it is found it comes out of the loop and then increments the number(that is length of the string wc) you get by 1 as we start numbering by 0 in c language otherwise loop continues until null character is found.

This is what I understood.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...