pointer to char, strings

V

Vasko Altiparmakov

hello

I am very new to C++ (and C) so i need something to clear up.

if you write

char ch[40];

you can uste it to store null terminated strings with length of 39
chars ascII.

if you code

char *ch;

what is the length of the n.string you can handle, for a 16bit
compiler like Turbo C++ 2.0?
is it 254 + zero termination?
will an omission to write a 255+ string corupt other variables space?

how do i print the address of a char variable?

char c;
cout << &c; will do the same as trying to write out a string since &c
is pointer to char.

and please one more thing. what is happening here.

#include <iostream.h>

char ch;
char *cp;

int main()
{ cp = &ch;
cp = "hello"; // i think this changes the point address of
the pointer but where ?

cout << ch << endl;
cout << cp << endl;
ch = 'u';
cout << ch << endl;
cout << *cp << endl; // strange?
cp = &ch;
cout << *cp << endl; // this is what i expect
return 0;
}

thanks in advance.

Vasko
 
S

Sam Holden

hello

I am very new to C++ (and C) so i need something to clear up.

if you write

char ch[40];

you can uste it to store null terminated strings with length of 39
chars ascII.

Not necessarily ASCII, but yes.
if you code

char *ch;

what is the length of the n.string you can handle, for a 16bit
compiler like Turbo C++ 2.0?
is it 254 + zero termination?
will an omission to write a 255+ string corupt other variables space?

As much memory you can allocate and have ch point too.

how do i print the address of a char variable?

Cast it to void* :

std::cout said:
char c;
cout << &c; will do the same as trying to write out a string since &c
is pointer to char.

and please one more thing. what is happening here.

#include <iostream.h>

#include <iostream>

It's been a pretty long time now... (and a using namespace std; if you
don't want to use std::cout below)

char ch;
char *cp;

int main()
{ cp = &ch;
cp = "hello"; // i think this changes the point address of
the pointer but where ?

It causes cp to point to the the place in memory where the compiler has
arranged for [ 'h' 'e' 'l' 'l' 'o' '\0' ] to be.
cout << ch << endl;
cout << cp << endl;
ch = 'u';
cout << ch << endl;
cout << *cp << endl; // strange?

That should output 'h', since cp does not point to ch and
hence changing ch will not have any effect on the string
pointed to by cp.
 
C

Clark Cox

hello

I am very new to C++ (and C) so i need something to clear up.

if you write

char ch[40];

you can uste it to store null terminated strings with length of 39
chars ascII.

if you code

char *ch;

what is the length of the n.string you can handle,

It can point to any string, which basically means that it can point to a
string that is as long as the maximum amount of memory you can allocate
minus one.
for a 16bit
compiler like Turbo C++ 2.0?
is it 254 + zero termination?
will an omission to write a 255+ string corupt other variables space?

how do i print the address of a char variable?

char c;
cout << &c; will do the same as trying to write out a string since &c
is pointer to char.

and please one more thing. what is happening here.

#include <iostream.h>

char ch;
char *cp;

int main()
{ cp = &ch;
cp = "hello"; // i think this changes the point address of
the pointer but where ?

It changes it to point to wherever the string "hello" is stored by
your compiler (i.e. it will point somewhere, and you shouldn't concern
yourself with where). Note, that you are not allowed to modify the
memory that cp now points to (i.e. something like: *cp = 'a'; is
undefined behavior)
cout << ch << endl;
cout << cp << endl;
ch = 'u';
cout << ch << endl;
cout << *cp << endl; // strange?

This should output 'h', which is the first character of the string
"hello"
cp = &ch;

This changes cp so that it points to ch
cout << *cp << endl; // this is what i expect

Because cp points to ch, *cp == ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top