473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer to char, strings

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
Jul 22 '05 #1
2 2084
On 11 Jan 2004 10:47:03 -0800, Vasko Altiparmakov <sp*********@yahoo.com> wrote:
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 << static_cast<void*>(&c) << std::endl;

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.

--
Sam Holden
Jul 22 '05 #2
In article <b0*************************@posting.google.com> ,
sp*********@yahoo.com (Vasko Altiparmakov) wrote:
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
return 0;
}

thanks in advance.

Vasko


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
5
by: ramu | last post by:
Hi, Could anyone please tell me how to dereference a pointer to an array of pointers? Regards
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.