473,790 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of pointers + memory Location

I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.
My colleague argues that in flat bed memory this should not be done. A
new should be used to allocate the memory and then point to it. Of
course he didnt do the best job explaining, hence my question to you
guys, if you can shed some more light on this.

Thanks in anticipation
Mar 5 '08 #1
9 1983
On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.
yes
My colleague argues that in flat bed memory
in *what*?
this should not be done.
why not?
A
new should be used to allocate the memory and then point to it.
why? Why waste the cost of a new (and they are quite expensive)
when it isn't necessary?

Of
course he didnt do the best job explaining, hence my question to you
guys, if you can shed some more light on this.
I've little sympathy with people who give apparently motiveless
technical advice. If he knows the reason why doesn't he tell you?
--
Nick Keighley
Mar 5 '08 #2
Nick Keighley wrote:
On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
>I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.

yes
Except that it shouldn't compile since "John" is an array of const
char and 'str[1]' is a pointer to non-const char. There is no
conversion without a cast. The intent probably was to declare 'str'
as 'char const* str[10];'
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 5 '08 #3
On Mar 5, 9:57 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Nick Keighley wrote:
On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
I have more of a conceptual question now. Let us say I do this:-
char *str[10]; --create an array of pointers
str[1]= "John";
I thought this would automatically put John at some memory space and
point str[1] to it.
yes

Except that it shouldn't compile since "John" is an array of const
char and 'str[1]' is a pointer to non-const char. There is no
conversion without a cast. The intent probably was to declare 'str'
as 'char const* str[10];'
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
It does compile as long as it is inside main or any function. So may
be it does a type conversion.
Mar 5 '08 #4
On Mar 5, 9:57 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Nick Keighley wrote:
On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
I have more of a conceptual question now. Let us say I do this:-
char *str[10]; --create an array of pointers
str[1]= "John";
I thought this would automatically put John at some memory space and
point str[1] to it.
yes

Except that it shouldn't compile since "John" is an array of const
char and 'str[1]' is a pointer to non-const char. There is no
conversion without a cast. The intent probably was to declare 'str'
as 'char const* str[10];'
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
It does compile as long as it is inside main or any function. So may
be it does a type conversion.
Mar 5 '08 #5
Victor Bazarov wrote:
Nick Keighley wrote:
>On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
>>I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.
yes
Agreed, "John" now lives in static storage.
Except that it shouldn't compile since "John" is an array of const
char and 'str[1]' is a pointer to non-const char. There is no
conversion without a cast.
There is such a conversion, although it is deprecated. From
[conv.array]: "This conversion is considered only when there is an
explicit appropriate pointer target type, and not when there is a
general need to convert from an lvalue to an rvalue."
The intent probably was to declare 'str'
as 'char const* str[10];'
I believe any attempt to modify the string is undefined behavior, so (as
you suggest) the OP is surely better off with char const*. Even better,
IMO, would be replacing the raw array of raw pointers with a vector (or
tr1 array) of std::string.
Mar 5 '08 #6
"Victor Bazarov" <v.********@com Acast.netwrites :
Nick Keighley wrote:
>On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
>>I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.

yes

Except that it shouldn't compile since "John" is an array of const
char and 'str[1]' is a pointer to non-const char. There is no
conversion without a cast. The intent probably was to declare 'str'
as 'char const* str[10];'
No cast is required, for compatibility with C (it's a, deprecated,
standard conversion, and only applies to string literals).

No diagnostic is required, but decent implementations will issue one
anyway.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Mar 5 '08 #7
On 2008-03-06 07:07:36 -0500, utab <um********@gma il.comsaid:
char * p1=0,p2=0; // **
This is equivalent to

char *p1 = 0;
char p2 = 0;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Mar 6 '08 #8
On Mar 6, 1:59 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-03-06 07:07:36 -0500, utab <umut.ta...@gma il.comsaid:
char * p1=0,p2=0; // **

This is equivalent to

char *p1 = 0;
char p2 = 0;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Ah yes, I missed that. Thanks
Mar 6 '08 #9
On 6 Mar, 13:07, utab <umut.ta...@gma il.comwrote:

[snip]
>
And the last question is that I have an array of pointers to char, how
can I get the start address of each char array that are pointed by the
pointers in the array? This is a bit related to my 1st question where
I thought I can get it with &(str[0][0]), but apparently there is sth
I am missing.
If you have an array of pointers to char, then the start address of
each char array is the pointer itself (at least unformally). So
&(str[0][0]) == str[0]

DP
Mar 6 '08 #10

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

Similar topics

1
19609
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we grow a vector it may copy all of its elements to different memory location to accomodate the new vector size. Is it possible that at some point an element of fixed array e.g. a_vStr gets invalid or
1
2076
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of data-types of my choosing (eg an array of char, short, or int). The application has to do with generating checksum-type values for files or strings, so speed is important, as I just want to quickly get this value then move on to the next task. As...
58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
6
6614
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using oci8 routines. The output resides in a structure defined within the sub-routine. Now I want this structure to be returned to main program so that I can assing output data to variables in main program and do some manipulation. Can any body guide...
4
8827
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
8
3688
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
24
3830
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
8
1836
by: ais523 | last post by:
I've checked the FAQ for this and couldn't find the answer. Is the following code snippet portable? int a; a=6; printf("%d\n",(*a)); This prints "6" on my compiler. I've been told it's always legal, but it seems slightly suspect to me. Can you really go out-of-bounds like this on one of the 'sub-arrays' a, a, etc.?
3
2155
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 which is part of the "atoms" structure that is passed into the function from the outside. I have not yet found where it is allocated but I'm reasonably sure from other chunks of this code that it was by:
23
7420
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
0
9666
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...
0
10413
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
9986
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
9021
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
7530
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
6769
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
5422
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...
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.