char array and pointer

Pages: 123
So I made a char array and I have to make a pointer. The pointer moves through the array to print out a line of text. I need to make a for loop for this. How do I use '\0' in order to accomplish this or am I way off here?
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    const char *s = "hello world";
    
    for (const char *p = s; *p; ++p)
        std::cout << *p;
    std::cout << '\n';
}

^^ remember that \0 char is just the integer 0 and that a char is just a 1 byte int type. so char x = 0; is the same as char x = '\0'; Now recall that 0 is false, else true, in c++. so in the above, *p means that *p is true, which means that *p is not zero...
Last edited on
oh crap. thanks. I got this piece of code. Trying to get the functions to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
    char *wordsptr = words;


    for(int = 0; i < 256; i++)//stuck here but you see what I mean. 
      {

       }



}
/***************************************************************************************************************/
void printReverse(char words[])//Prints data backwards
{
    char *wordsptr = words;

    cout<<"Reverse Order"<<endl;

    for(int i = 0; i < '\0'; i++)
    {
        words--;
        cout<<words[i]<<endl;
    }



}
/**********************************************************************************************************************/



i < '\0'
You're not comparing i against the null character, i is just an int.
You supposed to compare the character in the word itself against '\0'.

e.g. this is what your char array might look like:
char:   'H'   'e'   'l'   'l'   'o'   '!'   '\0'
index:   0     1     2     3     4     5    (6)


my_arr[4] == 'o'
my_arr[6] == '\0'

See how the last character is the '\0' character? That's when you know to stop iterating.

for(int = 0; i < 256; i++) What is 256? If you want the length of your string, use strlen(words) Also your iteration variable needs a name, e.g. "i".
Last edited on
so make the loop like this:

1
2

 for(int i = 0; i < strlen(words; i++))


Excuse my ignorance, but when you say iteration variable, what do you mean?
I meant your int i.

That's not correct syntax for a for loop.

A for loop is:
1
2
3
4
for ( {initial setup}; {condition to loop on}; {increment or other action} )
{

}


1
2
3
4
for (int i = 0; i < strlen(words); i++)
{

}
Last edited on
Oh and the strlen. I read it in my book but its all gibberish to me. I would need to initialize it first?
Not sure what your question is, ask it again but be very clear where the issue is.
Last edited on
Oh ok, what would you change it to then? I hate asking for help. I just want to learn this stuff. I do appreciate the help. I guess I don't know what question to ask. I have been learning the for loop like that. Perhaps I should show you my code as a whole:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Preprocessors*/
#include <iostream> //includes basics
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>
//Chris Rachal ITP 232 Lesson 3 assignment

using namespace std;
//Function Prototypes
void getText(char words[]);
void printOriginal(char words[]);
void printReverse(char words[]);




int main()
{
   /****************************************************************************
   *This is such a fun class. I really enjoy learning all there is to know about
   *C++. Array stores this text.
   *****************************************************************************/

    char words[256];//Array to hold text
    getText(words);//Calls the function to store data into the array.
    printOriginal(words);
    printReverse(words);




    return 0;
}
//function definitions
//Function definition to have the user type text and store it into the array.
void getText(char words[])
{

    cin.getline(words, 256);//saves the data entered by the user.

    }
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
    char *wordsptr = words;

 for(int i = 0; i < strlen(words); i++))


}
/***************************************************************************************************************/
void printReverse(char words[])//prints data in reverse
{
    char *wordsptr = words;

    cout<<"Reverse Order"<<endl;

    for(int i = 0; i < strlen(words); i++)
    {
        words--;
        cout<<words[i]<<endl;
    }



}
/**********************************************************************************************************************/


I know if doesn't look like much, but I work on it every day lol. I start erasing stuff and going back over it again and again.
My advice would be to slow down and make sure your syntax is correct before anything else.
Your loop on line 49 still has an extra right parentheses, ). And the for loop has no body.

Give it a body
1
2
3
4
for (int i = 0; i < strlen(words); i++)
{
    // print wordsptr[i]
}


For lines 60 - 63, I would remove line 62, and instead of starting from int i = 0, reverse the direction you're iterating for:
- start at an index i of strlen(words) - 1
- loop on i >= 0
- decrement (as opposed to increment) i each loop.
Last edited on
Thanks @Ganado I'm trying. You guys make it seem like second nature. I erased the one that had no body, because I couldn't figure that part out. I did at first but once. I started working on the reverse. It didn't compile right.
Thanks, I feel like I'm hopeless. Did you mean this:

1
2
3
for( i = strlen(word) -1; i < 0; i--);

That's very close.
1. Declare i within your for loop (int i instead of just i).
2. The second statement of the for loop is the condition to keep looping on.
It is what must be true to loop again.
3. Don't put a semi-colon at the end of the for loop. Put a body { }

i is initially some positive number, so it needs to loop on i being greater or equal to 0.
Last edited on
ah. What about this:

1
2
3
4
5
6
for(int i = strlen(words) -1; i < strlen(words); i--)
{
    cout<<words[i]<<endl;

}
Last edited on
That seems to work. Only I need to change something up. I have cout in both loops. When I build and run it will just print out the reverse function.
for(int i = strlen(words) -1; i < strlen(words); i--)

Suppose strlen(words) == 5.

This is doing:
1
2
3
4
for (int i = 4; i < 5; i--)
{

}

How will this end?
It would go down. My brain is mush right now. Sorry about that.
You guys make it seem like second nature.

Many of us have done coding for many decades. Its not like we woke up last week and all became clear from a vivid dream or something :)
Yeah I understand. I hope to get on that level one day. I don't know, it just seems like I'm only one lost lol.
Pages: 123