473,789 Members | 3,157 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 #1
35 20967
"Ying Yang" <Yi******@hotma il.com> wrote in message
news:3f******** @news.iprimus.c om.au...
Hi,

whats the difference between:

char* a = new char;

char* b = new char[10];
'a' and 'b' are both pointers to a char. That is, they each contain the
address in memory of a char. In the case of 'b', there happens to be another
9 chars after the first one, but it is up to the programmer to be aware of
that (including using "delete[]" for 'b' and just "delete" for 'a' later
when you delete them). Once created, the compiler treats both pointers the
same.
char c [10];


'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. But 'c' itself is an array, not a pointer, so it is a different
type from 'a' and 'b'.

DW

Jul 19 '05 #2
Ying Yang wrote:
Hi,

whats the difference between:

char* a = new char;
"a" is a pointer to a character, initialized by the expression new char.
The expression new char allocates memory for one character from the free
store and (according to the current wording of the standard) initialized it
by doing nothing. So "a" will point at that "random" character in the free
store (sometimes called heap).

If new char cannot allocate memory for one object of type char it will throw
an std::bad_alloc exception, which will be either caught by an appropriate
ctach-handler or will cause the system to call a function named terminate(),
which will by default call a function named abort() and that one will stop
the programs' execution.
char* b = new char[10];
"b" is a pointer to a character, initialized by the expression new char[10].
The expression new char[10] allocates a block of continuous memory for 10
character from the free store and (according to the current wording of the
standard) initialized it by doing nothing. After initialization "b" will
point at the first of those 10 "random" characters in the free store
(sometimes called heap).

Rest is the same as before.
char c [10];


This one can be many things. So let's skip initialization and meaning
(because it can be namespace scope or automatic or member declaration) and
let's just go for the type.

"c" is an array of 10 characters. This means that it has the type: array of
10 characters. When using "c" in certain expressions it will (so-called)
decay into a pointer to the first element of that array. In such a case it
will behave much like "b" before, except that the memory area belonging to
"c" is not necessarily from the free-store:

c[2]; // third element of c
b[2]; // third element of b

It is important to note that while many many times "c" will behave as a
pointer to the first element of the array it represents, it is not one. It
just can behave as one (in certain expressions) for our convenience.

--
Attila aka WW
Jul 19 '05 #3


Ying Yang wrote:

Hi,

whats the difference between:

char* a = new char;
a
+-------+ +---+
| o------------------>| |
+-------+ +---+
char* b = new char[10];

b
+-------+ +---+---+---+---+---+---+---+---+---+---+
| o------------------>| | | | | | | | | | |
+-------+ +---+---+---+---+---+---+---+---+---+---+
char c [10];


c
+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+

HTH

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4
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.


Except for a few well-documented cases, such as when used with the
sizeof and address-of (&) operators.


Brian Rodenborn
Jul 19 '05 #5
"Default User" <fi********@com pany.com> wrote in message
news:3F******** *******@company .com...
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.


Except for a few well-documented cases, such as when used with the
sizeof and address-of (&) operators.


But neither of these cases is using 'c' where a char * is expected, is it?

DW

Jul 19 '05 #6
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*.

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

Jul 19 '05 #7
Kevin Goodsell wrote:
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*.


Not exactly. Template argument deduction will not (AFAIK) decay it into a
pointer.

--
WW aka Attila
Jul 19 '05 #8
White Wolf wrote:

Not exactly. Template argument deduction will not (AFAIK) decay it into a
pointer.


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.

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

Jul 19 '05 #9
David White wrote:

"Default User" <fi********@com pany.com> wrote in message
news:3F******** *******@company .com...
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.


Except for a few well-documented cases, such as when used with the
sizeof and address-of (&) operators.


But neither of these cases is using 'c' where a char * is expected, is it?

I'm pointing out the cases where no conversion takes place. Many people
are unaware of these exceptions, you'll often see the phrase, "the name
of the array is a pointer to the first element". Naturally, the
operators mentioned take any type.

Brian Rodenborn
Jul 19 '05 #10

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
2539
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
3423
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
3981
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
10091
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
4065
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
3226
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
9996
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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
9020
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...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
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...
1
4092
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
3
2909
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.