Pointers Homework

Pages: 1234
the zero character is in the 15th place of the strings, which means you have to write string1[14] and string2[14] to access it. Also, the zero character is not printable, so you'll have to cast it to an int to actually see it, try this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
    char string1[]="Hello, World!\n";
    char *string2="Hello, World!\n";

    cout << int(string1[14]) << endl;
    cout << int(string2[14]) << endl;
    
    system("pause");
    return 0;
}


basically, it's not a zero character, it's just a zero...
Last edited on
It's not a zero, it's a null. 0 is not the same as \0.
i know that, i intentionally wrote 15 so access the 16th.. which would exceed the valid index for the strings given.. subtracting 0 also give the numerical value of the character isn't it..

maybe you didn't get what i mean, sorry if it's not clear.

@tummychow
it show that null is zero http://www.asciitable.com/ can you clear this up, i thought null = 0
Last edited on
@tummychow

0 is the same as '\0'
'0' is not the same as '\0'

see for yourself...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
    
    char c1=0;
    char c2='0';
    char c3='\0';
    
    cout << "printing 0:\t" << int(c1) << endl;
    cout << "printing \'0\':\t" << int(c2) << endl;
    cout << "printing \'\\0\':\t" << int(c3) << endl;
    
    system("pause");
    return 0;
}
Last edited on
The character 0 is not the same as the character \0. That's exactly what I said. It's also what you said:
the zero character
Last edited on
@blackcoder41
oh, ok, sorry for implying that you don't know how to determine the boundary of an array :P
but what did you want to do with that value? what information would it give you?
Last edited on
@tummychow
ok, then we're both right :D
maybe the value of string2[14] is zero by coincidence.. don't you think?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
    char string1[]="Hello, World!\n";
    char *string2="Hello, World!\n";
    
    //works :)
    string1[15] = 65;
    cout << (int)string1[15] << endl;
    
    //crap :(
    string2[15] = 65;
    cout << (int)string2[15] << endl;
    return 0;
}
The character 0 is not the same as the character \0.
Yes, it is.

maybe the value of string2[14] is zero by coincidence.
Nope.

Line 9 is overflowing string1.
@blackcoder41

look, you can't modify anything using the string2 var, coz "Hello, World!\n\0" is stored as a constant. if u try string2[0]=65 or string2[7]=65 it still won't happen. The statement string2[15]=65 crashes the program not because you exceed the limits of the array but because you try to modify something that is constant.
Last edited on
@helios & tummychow

tymmychow:
The character 0 is not the same as the character \0.

helios:
Yes, it is.


I believe what tummychow means is
'0' is not the same as '\0'
(I assume that the word "character" means we have to put the next word into single quotes)

so, he is right... ok, let's stop this, we all say the same thing, the one that is correct.
The importance of proper typing.
whats the difference then?
I told him something about the memory reserved by the compiler when he encounters these statements (see previous posts on this topic) but he told me it wasn't the answer he wanted...
i guess no one knows the answer :(
it's on page 2 of this topic... his answer was

#4 has nothing to do with the amount of memory used. It's about the nature of the object named 'string'. I want to know what it is in either case and what changes in the running program when it is created.


well, we could say that in the first case the type of string is char[15] while in the second it's char*
As for the changes in the running program... I m not sure what he wants us to say... In both cases the literal "Hello, World!\n" is stored somewhere. And then comes the initialization of the array or the pointer. The array has its elements initialized one by one (by copying the characters from the string literal), and the pointer is simply set to point to the beginning of the string literal. What else?
Last edited on
Meh. Close enough.
LOL, I almost said everything but he barely lets me pass the test! What kind of a teacher are you?! :D
Last edited on
Did you see Kill Bill? That kind.
HAHAHAHAHAHAHA XD
Topic archived. No new replies allowed.
Pages: 1234