473,803 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char* and char

Hi,

whats the difference between:

char* a = new char;

char* b = new char[10];

char c [10];
Regards
BN
Jul 19 '05
35 20969
Kevin Goodsell wrote:
Good point. In C what I said holds true, but I was never very sure if
C++ added other contexts where the "decay to pointer" would be bypassed.
No one bothered to point this out until now, and it simply never
occurred to me.

I wasn't either, so I hedged my bets :)

I'm not as strong on templates as I should be, I wasn't sure if you
could instantiate with array types or not. They'd be tricky, because any
resulting variables in a class would not be modifiable lvalues and all
that.

Brian Rodenborn
Jul 19 '05 #11
Default User wrote:
I wasn't either, so I hedged my bets :)

I'm not as strong on templates as I should be, I wasn't sure if you
could instantiate with array types or not. They'd be tricky, because
any resulting variables in a class would not be modifiable lvalues
and all that.


Really? How come? What do you mean by that?

--
WW aka Attila
Jul 19 '05 #12
Kevin Goodsell <us************ *********@never box.com> wrote in message
news:DK******** *********@newsr ead4.news.pas.e arthlink.net...
David White wrote:

'c' is an array of 10 chars. If you use 'c' where a char * is expected, the compiler will create the char * by taking the address of the first char in the array.


A stronger statement is called for here. If you use 'c' for any purpose
other than as the operand for the address-of or sizeof operator, the
compiler will convert it to char*. It does not have to occur in a
context that calls for a char*.


Perhaps this is a bit too strong. It's liable to cause some people to think
of an array more as a pointer than as an array.

What about
char ch = c[i];
?
Is this using 'c' as an array or a pointer? The result is the same whether
'c' is an array or a pointer, but I'd prefer to think of the pointer case as
behaving like an array than the array case behaving like a pointer.

What happens to the array here?
struct S
{
char c[10];
};

void f(S &s1, S &s2)
{
s1 = s2;
}

There's no pointer behaviour in this case.

I think it's better to list the cases where one type decays to another than
to list the exceptions.

DW

Jul 19 '05 #13
David White wrote:
Perhaps this is a bit too strong. It's liable to cause some people to
think of an array more as a pointer than as an array.

What about
char ch = c[i];
?
Is this using 'c' as an array or a pointer?
Pointer.
The result is the same
whether 'c' is an array or a pointer, but I'd prefer to think of the
pointer case as behaving like an array than the array case behaving
like a pointer.
There is no array arithmetics in C++, but there is pointer arithmetics.
What happens to the array here?
Here you do not use the array.
struct S
{
char c[10];
};

void f(S &s1, S &s2)
{
s1 = s2;
}

There's no pointer behaviour in this case.
Because you do not use the name of the array.
I think it's better to list the cases where one type decays to
another than to list the exceptions.


Except if the exceptions are way less.

BTW if you do not believe me that in you example case above "c" indeed
decayed to a pointer try the following:

// Warning1 Untested code typed with flue and migrene
#include <iostream>
typedef int not_void_ever;

not_void_ever main() {
char const gree_thing[] = "Hello world!";
std::cout << "char 6: " << 6[gree_thing] << std::endl;
}

Compile it. It will. Run it. It will and it will print "char 5: w".

Why? Because 6 is an array? Nope. But because gree_thing decays into a
pointer to the first element of the character array. And then what we have
is:

something[something]

which translates to

*(something+som ething)

in our case

*(6+gree_thing)

--
WW aka Attila
Jul 19 '05 #14
Default User wrote:
Kevin Goodsell wrote:

Good point. In C what I said holds true, but I was never very sure if
C++ added other contexts where the "decay to pointer" would be bypassed.
No one bothered to point this out until now, and it simply never
occurred to me.
I wasn't either, so I hedged my bets :)

I'm not as strong on templates as I should be,


It seems like few people are. I know I'm not. It's a complicated topic.
I wasn't sure if you
could instantiate with array types or not. They'd be tricky, because any
resulting variables in a class would not be modifiable lvalues and all
that.


I'm not sure how it would work either, outside of things like this:

template<class T, size_t N>
void foo(T (&array)[N])
{
}

I'm not sure if this counts or not (it's what I thought of when I read
Attila's message, but maybe he was talking about something else). But I
think it points to another situation where arrays don't decay to
pointers - binding to a reference:

int a[20];
int (&r)[20] = a;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #15
David White wrote:

Perhaps this is a bit too strong. It's liable to cause some people to think
of an array more as a pointer than as an array.

What about
char ch = c[i];
?
Is this using 'c' as an array or a pointer?
A pointer, of course.

What happens to the array here?
struct S
{
char c[10];
};

void f(S &s1, S &s2)
{
s1 = s2;
}

The conversion applies when the array is referred to by name. It's often
stated something like "The name of an array becomes a pointer to the
first element of the array in most case. The exceptions are..."
I think it's better to list the cases where one type decays to another than
to list the exceptions.


But there are far more cases where it becomes a pointer - that's the
norm. I doubt I could list them all. In C there are only the 2 cases
where it doesn't. We're still discussing what additional cases are added
in C++.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #16
White Wolf <wo***@freemail .hu> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...
David White wrote:
Perhaps this is a bit too strong. It's liable to cause some people to
think of an array more as a pointer than as an array.

What about
char ch = c[i];
?
Is this using 'c' as an array or a pointer?
Pointer.
The result is the same
whether 'c' is an array or a pointer, but I'd prefer to think of the
pointer case as behaving like an array than the array case behaving
like a pointer.


There is no array arithmetics in C++, but there is pointer arithmetics.
What happens to the array here?


Here you do not use the array.


Yes I do, implicitly.
struct S
{
char c[10];
};

void f(S &s1, S &s2)
{
s1 = s2;
}

There's no pointer behaviour in this case.


Because you do not use the name of the array.


Does it really matter? The array is being used in the code above, and not
like a pointer. The contents of s1.c are replaced.
I think it's better to list the cases where one type decays to
another than to list the exceptions.


Except if the exceptions are way less.

BTW if you do not believe me that in you example case above "c" indeed
decayed to a pointer try the following:

// Warning1 Untested code typed with flue and migrene
#include <iostream>
typedef int not_void_ever;

not_void_ever main() {
char const gree_thing[] = "Hello world!";
std::cout << "char 6: " << 6[gree_thing] << std::endl;
}

Compile it. It will. Run it. It will and it will print "char 5: w".

Why? Because 6 is an array? Nope. But because gree_thing decays into a
pointer to the first element of the character array. And then what we

have is:

something[something]

which translates to

*(something+som ething)

in our case

*(6+gree_thing)


So, an array, unlike a pointer, contains no address and some number of
actual characters, yet we are to think of it as an address and no
characters. And a pointer, which contains just an address and no characters,
we are to think of as an array of characters (for the purposes of array[i]
anyway). It's not surprising that newbies have to ask lots of questions
here.

Nothing above surprises me, though I'd forgotten the quirky reversal of
array and index that I now remember seeing years ago, but for practical
purposes I'll stick to thinking of an array as an array and a pointer as a
pointer. Below is one reason why:

char c[10];
char d[10];
char *p = &c[0];
char *q = &d[0];

p = q; // okay
p = d; // okay
c = d; // error

This is another exception.

DW

Jul 19 '05 #17
Kevin Goodsell <us************ *********@never box.com> wrote in message
news:bg******** *********@newsr ead4.news.pas.e arthlink.net...
David White wrote:
The conversion applies when the array is referred to by name. It's often
stated something like "The name of an array becomes a pointer to the
first element of the array in most case. The exceptions are..."


I would think that people, newbies in particular, are interested in all the
differences betweeen pointers and arrays, not just those where the name is
used. I can see no good reason to single out uses of the name alone.
I think it's better to list the cases where one type decays to another than to list the exceptions.


But there are far more cases where it becomes a pointer - that's the
norm. I doubt I could list them all. In C there are only the 2 cases
where it doesn't. We're still discussing what additional cases are added
in C++.


If you list the exceptions and you miss one, the statement "An array is
treated as a pointer except..." becomes a false statement. But if you say,
"An array decays to a pointer where a pointer is expected," you at least
aren't making a false statement if there are other cases as well. So far the
exceptions in C++ are:
sizeof, address of, template parameter, default assignment of structs and
classes, and that an array is not an l-value.

That's quite a few, and are we certain there aren't more?

DW

Jul 19 '05 #18
David White wrote:
What happens to the array here?
Here you do not use the array.


Yes I do, implicitly.


No, you are not. Using the array in this context (decaying) means using the
arrays name. It is not the array which decays, it is name.
Because you do not use the name of the array.


Does it really matter?


Yes it does. It is nto the array decaying into a pointer: its name does.
So, an array, unlike a pointer, contains no address and some number of
actual characters, yet we are to think of it as an address and no
characters.
No. We are talking about array *names* and not arrays.
And a pointer, which contains just an address and no
characters, we are to think of as an array of characters (for the
purposes of array[i] anyway). It's not surprising that newbies have
to ask lots of questions here.
Nope. Array *names* behave like pointers to the arrays first element - most
of the cases. Pointers _never_ behave like arrays.
Nothing above surprises me, though I'd forgotten the quirky reversal
of array and index that I now remember seeing years ago, but for
practical purposes I'll stick to thinking of an array as an array and
a pointer as a pointer.


No one asked you to stop think about an array as an array and a pointer as a
pointer. But please also think about the _name_ of the array as one which ,
in certain contexts, can decay into naming a pointer rvalue. Not ever as an
lvalue as you example tried to show.

--
WW aka Attila
Jul 19 '05 #19
David White wrote:
I would think that people, newbies in particular, are interested in
all the differences betweeen pointers and arrays, not just those
where the name is used. I can see no good reason to single out uses
of the name alone.
You mean that we should not mention one very important rule of the language
just because at first sight it differs from other languages?
If you list the exceptions and you miss one, the statement "An array
is treated as a pointer except..." becomes a false statement.
It is _always_ a false statement. And array is *never* treated as a
pointer. The *name* of the array *variable* can behave as or decay into a
pointer.
But if
you say, "An array decays to a pointer where a pointer is expected,"
you at least aren't making a false statement
You are. The array never decays into anything.

T foo[S];

Suppose the above is an array declaration. The array is a variable. It has
an object part (the associated continuous region of storage) and a name
part, which we use to refer to that storage and a type part, which tells
what is in that storage, how big that storage is etc.

The array is the object (the storage) with its type. The word foo is the
name of the variable. We use the name of the variable to refer to the
storage. Now in some cases, foo refers to that storage as a pointer to its
first element, while in another cases it represent the whole storage with
all the elements in it.
So far the exceptions in C++ are:
sizeof, address of, template parameter,
Yes.
default assignment of structs
and classes,
No. In the default assignment of classes the _name_ of the array is not
used.
and that an array is not an l-value.
That's quite a few, and are we certain there aren't more?


The standard lists those.

--
WW aka Attila
Jul 19 '05 #20

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

Similar topics

9
2210
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my attempt at it, but it seems to be lacking... I'm aware that strdup() is nonstandard (and a bad idea for C++ code) - please just bear with me: /* Assume relevant headers are included */ class char_ptr {
5
9745
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an array of char > > typedef struct { > > char name; > > int age; > > int id; > > } person; > >
5
2540
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if I allocate memory in the function, on the return of this function I see garbage.. here is my...
2
3426
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has raised a doubt in my mind on the same issue. Either through ignorance or incompetence, I've been unable to resolve some issues. 6.4.4.4p6 states...
5
3983
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
10093
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
18
4067
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
3227
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
6800
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
9998
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.