Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9060 > unrolled thread
| Started by | suresh <suresh.amritapuri@gmail.com> |
|---|---|
| First post | 2011-08-18 21:04 -0700 |
| Last post | 2011-08-19 19:39 +0100 |
| Articles | 20 on this page of 25 — 10 participants |
Back to article view | Back to comp.lang.c++
concatenate variable number of arguments and types to a char * suresh <suresh.amritapuri@gmail.com> - 2011-08-18 21:04 -0700
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 06:26 +0200
Re: concatenate variable number of arguments and types to a char * Felix Bytow <felix.bytow@informatik.tu-chemnitz.de> - 2011-08-19 06:50 +0200
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 08:04 +0200
Re: concatenate variable number of arguments and types to a char * PaulR <pchristor@yahoo.co.uk> - 2011-08-19 04:01 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 14:00 +0100
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 14:31 +0000
Re: concatenate variable number of arguments and types to a char * "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> - 2011-08-19 16:57 +0200
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 19:15 +0000
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 16:33 +0100
Re: concatenate variable number of arguments and types to a char * Juha Nieminen <nospam@thanks.invalid> - 2011-08-19 19:18 +0000
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 20:29 +0100
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:18 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 22:37 +0100
Re: concatenate variable number of arguments and types to a char * Ian Collins <ian-news@hotmail.com> - 2011-08-20 09:43 +1200
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 15:08 -0700
Re: concatenate variable number of arguments and types to a char * Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-19 16:14 -0400
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 13:54 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 22:01 +0100
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:20 -0700
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 14:21 -0700
Re: concatenate variable number of arguments and types to a char * Paul <pchristor@yahoo.co.uk> - 2011-08-19 11:03 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 19:23 +0100
Re: concatenate variable number of arguments and types to a char * red floyd <no.spam.here@its.invalid> - 2011-08-19 11:13 -0700
Re: concatenate variable number of arguments and types to a char * Leigh Johnston <leigh@i42.co.uk> - 2011-08-19 19:39 +0100
Page 1 of 2 [1] 2 Next page →
| From | suresh <suresh.amritapuri@gmail.com> |
|---|---|
| Date | 2011-08-18 21:04 -0700 |
| Subject | concatenate variable number of arguments and types to a char * |
| Message-ID | <14393cec-d1a9-4971-8a27-120e87b045a0@glegroupsg2000goo.googlegroups.com> |
Hi
How to write a function which would concatenate variable number of arguments of different type and return a char *. For example I need the function
f("his weight is = ", 51.5, "pounds");
to return the string "his weight is = 51.5 pounds".
thanks
suresh
[toc] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-08-19 06:26 +0200 |
| Message-ID | <j2koks$7f3$1@dont-email.me> |
| In reply to | #9060 |
On 19.08.2011 06:04, suresh wrote:
> Hi
> How to write a function which would concatenate variable number of arguments of
> different type and return a char *. For example I need the function
>
> f("his weight is = ", 51.5, "pounds");
>
> to return the string "his weight is = 51.5 pounds".
One simple way is like (off the cuff)
template< class T >
string toString( T const& v )
{
stringstream stream_;
stream_ << v;
return stream.str();
}
struct S
{
string s_;
public:
template< class T >
S& operator<<( T const& v )
{
s_ += toString( v );
return *this;
}
operator char const* () const
{
return s_.c_str();
}
};
It can be elaborated and, not the least, optimized.
Anyway, then, when you want to call a function
void foo( char const* s )
you can say things like
foo( S() << "his weight is = " << 51.5 << "pounds" )
Cheers & hth.,
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Felix Bytow <felix.bytow@informatik.tu-chemnitz.de> |
|---|---|
| Date | 2011-08-19 06:50 +0200 |
| Message-ID | <j2kq3j$3bg$1@bruford.hrz.tu-chemnitz.de> |
| In reply to | #9060 |
Am 19.08.2011 06:04, schrieb suresh:
> Hi
> How to write a function which would concatenate variable number of arguments of different type and return a char *. For example I need the function
>
> f("his weight is = ", 51.5, "pounds");
>
> to return the string "his weight is = 51.5 pounds".
>
> thanks
> suresh
Hi,
this might do, what you want:
#include <string>
#include <sstream>
#include <iostream>
template <typename Head>
std::string concat(Head const & head)
{
std::stringstream strm;
strm << head;
return strm.str();
}
template <typename Head, typename... Tail>
std::string concat(Head const & head, Tail... tail)
{
return (concat(head) + concat(tail...));
}
int main(void)
{
std::cout << concat("The answer is ", 42, "!") << std::endl;
return 0;
}
Ofcourse this code makes use of variadic templates which are part of
C++11. You will find that not all compilers support that already.
Using the old tools you will not be able to write a function that fits
your needs completely...
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-08-19 08:04 +0200 |
| Message-ID | <j2kud3$t4$1@dont-email.me> |
| In reply to | #9062 |
On 19.08.2011 06:50, Felix Bytow wrote:
> Am 19.08.2011 06:04, schrieb suresh:
>> Hi
>> How to write a function which would concatenate variable number of arguments of different type and return a char *. For example I need the function
>>
>> f("his weight is = ", 51.5, "pounds");
>>
>> to return the string "his weight is = 51.5 pounds".
>>
>> thanks
>> suresh
>
> Hi,
> this might do, what you want:
>
> #include<string>
> #include<sstream>
> #include<iostream>
>
> template<typename Head>
> std::string concat(Head const& head)
> {
> std::stringstream strm;
> strm<< head;
> return strm.str();
> }
>
> template<typename Head, typename... Tail>
> std::string concat(Head const& head, Tail... tail)
> {
> return (concat(head) + concat(tail...));
> }
>
> int main(void)
> {
> std::cout<< concat("The answer is ", 42, "!")<< std::endl;
> return 0;
> }
>
> Ofcourse this code makes use of variadic templates which are part of
> C++11. You will find that not all compilers support that already.
>
> Using the old tools you will not be able to write a function that fits
> your needs completely...
The C++98 solution I posted is more efficient, O(n) versus yours O(n^2),
at the cost of slightly different notation.
The C++0x machinery you're using can be used to wrap such a more
efficient solution in order to get conventional usage notation.
However I think the C++98 solution is OK as it is... ;-)
Cheers & hth.,
- Alf
PS: Please replace tab characters with spaces before posting. Many
editors do that with just a few keypresses.
[toc] | [prev] | [next] | [standalone]
| From | PaulR <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-08-19 04:01 -0700 |
| Message-ID | <f929eee7-9c0c-4510-8904-e3096a2a4062@h3g2000vbr.googlegroups.com> |
| In reply to | #9060 |
On Aug 19, 5:04 am, suresh <suresh.amritap...@gmail.com> wrote:
> Hi
> How to write a function which would concatenate variable number of arguments >of different type and return a char *. For example I need the function
The problem with this design is.. What does the char* point to?
Returning a char* to a string created inside a function is a bit
dangerous as the string will be out of scope when the function
returns.
Unless you are playing with dynamic allocation of char arrays.
You are safer to return a std::string object by value and then you can
easily use this as a char pointer by calling the c_str() member
function.
>
> f("his weight is = ", 51.5, "pounds");
>
> to return the string "his weight is = 51.5 pounds".
>
As an improvment over what Alf posted(i think):
#include <string>
#include <sstream>
#include<iostream>
class str_add{
public:
template<typename T>
str_add& operator <<(T t){ss<<t; return *this;}
std::string str(){return ss.str();}
private:
std::stringstream ss;
};
int main() {
std::string str = (str_add()<< "lets add..." << 5.6179 << " or
something.").str();
const char* cp1 = str.c_str();
//cp1 is ok, it points to a valid string.
const char* cp2 = (str_add()<< "lets add..." << 5.6179 << " or
something.").str().c_str();
//cp2 is not ok, it pointed to a temporary
}
Note you can make the operator anything you like( i.e + or += )
instead of <<.
If you send it into a function as a char* you can only use it in the
function, whereas if you send it in as a std::string it can be
returned and used both inside and outside the function, and is much
safer.
so you make a function that accepts a std::string argument and pass
it:
(str_add()<< "lets add..." << 5.6179 << " or something.").str()
Obviously with your own words and numbers.
And you can also return that string from the function if you want.
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-19 14:00 +0100 |
| Message-ID | <qsWdnfQII6Lww9PTnZ2dnUVZ8nSdnZ2d@giganews.com> |
| In reply to | #9069 |
On 19/08/2011 12:01, PaulR wrote: > On Aug 19, 5:04 am, suresh<suresh.amritap...@gmail.com> wrote: >> Hi >> How to write a function which would concatenate variable number of arguments>of different type and return a char *. For example I need the function > > The problem with this design is.. What does the char* point to? > Returning a char* to a string created inside a function is a bit > dangerous as the string will be out of scope when the function > returns. > Unless you are playing with dynamic allocation of char arrays. char* can never point to an array; it can, however, point *into* an array or one past the end of an array. :) HTH. /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-08-19 14:31 +0000 |
| Message-ID | <4e4e73b7$0$3105$7b1e8fa0@news.nbl.fi> |
| In reply to | #9071 |
Leigh Johnston <leigh@i42.co.uk> wrote: > char* can never point to an array; it can, however, point *into* an > array or one past the end of an array. :) What's the difference? (I'd say that since you can delete/free a dynamically allocated array via a pointer that points to its first element, the pointer in question *is* pointing to the array.)
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach:usenet@gmail.com> |
|---|---|
| Date | 2011-08-19 16:57 +0200 |
| Message-ID | <j2ltkf$n2b$1@dont-email.me> |
| In reply to | #9072 |
On 19.08.2011 16:31, Juha Nieminen wrote: > Leigh Johnston<leigh@i42.co.uk> wrote: >> char* can never point to an array; it can, however, point *into* an >> array or one past the end of an array. :) > > What's the difference? > > (I'd say that since you can delete/free a dynamically allocated array > via a pointer that points to its first element, the pointer in question > *is* pointing to the array.) Didn't you note the long trolling threads about this? Finally Someone(TM) took the time to quote from the standard some examples of using the "point to" terminology. However, that only stops new people from being suckered into the trolling debate, for a limited time. And there are always words and expressions that are commonly used but not in the standard. So, best leave it. Cheers, - Alf
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-08-19 19:15 +0000 |
| Message-ID | <4e4eb641$0$2823$7b1e8fa0@news.nbl.fi> |
| In reply to | #9073 |
Alf P. Steinbach <alf.p.steinbach:usenet@gmail.com> wrote: > On 19.08.2011 16:31, Juha Nieminen wrote: >> Leigh Johnston<leigh@i42.co.uk> wrote: >>> char* can never point to an array; it can, however, point *into* an >>> array or one past the end of an array. :) >> >> What's the difference? >> >> (I'd say that since you can delete/free a dynamically allocated array >> via a pointer that points to its first element, the pointer in question >> *is* pointing to the array.) > > Didn't you note the long trolling threads about this? I stopped following them quite at the beginning, so I have no idea what was "discussed" there...
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-19 16:33 +0100 |
| Message-ID | <IIqdne4J2fvPH9PTnZ2dnUVZ8oudnZ2d@giganews.com> |
| In reply to | #9072 |
On 19/08/2011 15:31, Juha Nieminen wrote: > Leigh Johnston<leigh@i42.co.uk> wrote: >> char* can never point to an array; it can, however, point *into* an >> array or one past the end of an array. :) > > What's the difference? > > (I'd say that since you can delete/free a dynamically allocated array > via a pointer that points to its first element, the pointer in question > *is* pointing to the array.) Think of it this way: instead of an array of char lets have a vector of char: std::vector<char> v(1); char* p1 = &v[0]; // will compile char* p2 = v; // will not compile Clearly p1 is a pointer to a vector element not a pointer to a vector; p1 points to a vector element; it doesn't point to a vector; critically pointer decay does not occur as it does with the array case. :) /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-08-19 19:18 +0000 |
| Message-ID | <4e4eb70d$0$2823$7b1e8fa0@news.nbl.fi> |
| In reply to | #9074 |
Leigh Johnston <leigh@i42.co.uk> wrote: > On 19/08/2011 15:31, Juha Nieminen wrote: >> Leigh Johnston<leigh@i42.co.uk> wrote: >>> char* can never point to an array; it can, however, point *into* an >>> array or one past the end of an array. :) >> >> What's the difference? >> >> (I'd say that since you can delete/free a dynamically allocated array >> via a pointer that points to its first element, the pointer in question >> *is* pointing to the array.) > > Think of it this way: instead of an array of char lets have a vector of > char: > > std::vector<char> v(1); > char* p1 = &v[0]; // will compile > char* p2 = v; // will not compile > > Clearly p1 is a pointer to a vector element not a pointer to a vector; > p1 points to a vector element; it doesn't point to a vector; critically > pointer decay does not occur as it does with the array case. I don't really understand. std::vector is not an array, it's a class. It happens to internally manage an array, but it nevetheless isn't itself one. For all we know it could be a class that prints the 99 bottles of beer rhyme. char* isn't a class (especially not one of type std::vector), nor even a pointer to one. Apples and oranges?
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-19 20:29 +0100 |
| Message-ID | <zqadnWvNgswYJNPTnZ2dnUVZ8qudnZ2d@giganews.com> |
| In reply to | #9088 |
On 19/08/2011 20:18, Juha Nieminen wrote: > Leigh Johnston<leigh@i42.co.uk> wrote: >> On 19/08/2011 15:31, Juha Nieminen wrote: >>> Leigh Johnston<leigh@i42.co.uk> wrote: >>>> char* can never point to an array; it can, however, point *into* an >>>> array or one past the end of an array. :) >>> >>> What's the difference? >>> >>> (I'd say that since you can delete/free a dynamically allocated array >>> via a pointer that points to its first element, the pointer in question >>> *is* pointing to the array.) >> >> Think of it this way: instead of an array of char lets have a vector of >> char: >> >> std::vector<char> v(1); >> char* p1 =&v[0]; // will compile >> char* p2 = v; // will not compile >> >> Clearly p1 is a pointer to a vector element not a pointer to a vector; >> p1 points to a vector element; it doesn't point to a vector; critically >> pointer decay does not occur as it does with the array case. > > I don't really understand. std::vector is not an array, it's a class. > It happens to internally manage an array, but it nevetheless isn't itself > one. For all we know it could be a class that prints the 99 bottles of > beer rhyme. > > char* isn't a class (especially not one of type std::vector), nor even > a pointer to one. > > Apples and oranges? I was using it as an analogy; vector is semantically equivalent to an array albeit an array whose size can change. Pointing to a vector element is analogous to pointing to an array element just as pointing to a vector is analogous to pointing to an array. If you want a pointer to an array then use a pointer to an array: typedef int array_type[42]; array_type* pointer_to_an_array; int a[42]; pointer_to_an_array = &a; /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Paul <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-08-19 14:18 -0700 |
| Message-ID | <df693ca4-b453-42f1-b01f-37a6ad1b824d@m18g2000vbl.googlegroups.com> |
| In reply to | #9089 |
On Aug 19, 8:29 pm, Leigh Johnston <le...@i42.co.uk> wrote: > On 19/08/2011 20:18, Juha Nieminen wrote: > > > > > > > Leigh Johnston<le...@i42.co.uk> wrote: > >> On 19/08/2011 15:31, Juha Nieminen wrote: > >>> Leigh Johnston<le...@i42.co.uk> wrote: > >>>> char* can never point to an array; it can, however, point *into* an > >>>> array or one past the end of an array. :) > > >>> What's the difference? > > >>> (I'd say that since you can delete/free a dynamically allocated array > >>> via a pointer that points to its first element, the pointer in question > >>> *is* pointing to the array.) > > >> Think of it this way: instead of an array of char lets have a vector of > >> char: > > >> std::vector<char> v(1); > >> char* p1 =&v[0]; // will compile > >> char* p2 = v; // will not compile > > >> Clearly p1 is a pointer to a vector element not a pointer to a vector; > >> p1 points to a vector element; it doesn't point to a vector; critically > >> pointer decay does not occur as it does with the array case. > > > I don't really understand. std::vector is not an array, it's a class. > > It happens to internally manage an array, but it nevetheless isn't itself > > one. For all we know it could be a class that prints the 99 bottles of > > beer rhyme. > > > char* isn't a class (especially not one of type std::vector), nor even > > a pointer to one. > > > Apples and oranges? > > I was using it as an analogy; vector is semantically equivalent to an > array albeit an array whose size can change. Pointing to a vector > element is analogous to pointing to an array element just as pointing to > a vector is analogous to pointing to an array. > Why would you ever want to point to a vector? A vector has pass by value semantics, an array is quite the opposite. > If you want a pointer to an array then use a pointer to an array: > > typedef int array_type[42]; > array_type* pointer_to_an_array; > int a[42]; > pointer_to_an_array = &a; > You fail to distinguish between a pointer to an array and a pointer- type pointer to array. What you should have said is .... If you want a pointer OF TYPE pointer-to-array then use such a pointer type. The old pointer-TYPE argument works both ways because a pointer type void* doesn't point to a void. And for bonus points a pointer of type pointer-to-Base does not mean it doesn't point to a Derived. On a scale of 1-100 your fail is about 90%. That is 10% of readers may support your attempts to redefine programming terminology, the rest will probably realise tha they never read so much pish in their lives. :) Next you will be trying to suggest that newbies don't learn about arrays and instead should use std::vector or something ridiculous like that. Brainstorm.... You learned that way? :)
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-19 22:37 +0100 |
| Message-ID | <-tedna6sk4UWStPTnZ2dnUVZ7v2dnZ2d@giganews.com> |
| In reply to | #9095 |
On 19/08/2011 22:18, Paul wrote: > On Aug 19, 8:29 pm, Leigh Johnston<le...@i42.co.uk> wrote: >> On 19/08/2011 20:18, Juha Nieminen wrote: >> >> >> >> >> >>> Leigh Johnston<le...@i42.co.uk> wrote: >>>> On 19/08/2011 15:31, Juha Nieminen wrote: >>>>> Leigh Johnston<le...@i42.co.uk> wrote: >>>>>> char* can never point to an array; it can, however, point *into* an >>>>>> array or one past the end of an array. :) >> >>>>> What's the difference? >> >>>>> (I'd say that since you can delete/free a dynamically allocated array >>>>> via a pointer that points to its first element, the pointer in question >>>>> *is* pointing to the array.) >> >>>> Think of it this way: instead of an array of char lets have a vector of >>>> char: >> >>>> std::vector<char> v(1); >>>> char* p1 =&v[0]; // will compile >>>> char* p2 = v; // will not compile >> >>>> Clearly p1 is a pointer to a vector element not a pointer to a vector; >>>> p1 points to a vector element; it doesn't point to a vector; critically >>>> pointer decay does not occur as it does with the array case. >> >>> I don't really understand. std::vector is not an array, it's a class. >>> It happens to internally manage an array, but it nevetheless isn't itself >>> one. For all we know it could be a class that prints the 99 bottles of >>> beer rhyme. >> >>> char* isn't a class (especially not one of type std::vector), nor even >>> a pointer to one. >> >>> Apples and oranges? >> >> I was using it as an analogy; vector is semantically equivalent to an >> array albeit an array whose size can change. Pointing to a vector >> element is analogous to pointing to an array element just as pointing to >> a vector is analogous to pointing to an array. >> > Why would you ever want to point to a vector? Where did I say that you would *want* a pointer to a vector? I was using it in the analogy; a pointer to a vector is analogous to a pointer to an array; i.e. it is pointer to the container rather than what the container contains. > A vector has pass by value semantics, an array is quite the opposite. Indeed but this has no bearing on the analogy; the analogy is about pointers to containers versus pointers to container elements. > >> If you want a pointer to an array then use a pointer to an array: >> >> typedef int array_type[42]; >> array_type* pointer_to_an_array; >> int a[42]; >> pointer_to_an_array =&a; >> > > You fail to distinguish between a pointer to an array and a pointer- > type pointer to array. Noise. > > What you should have said is .... > If you want a pointer OF TYPE pointer-to-array then use such a pointer > type. More noise. > > The old pointer-TYPE argument works both ways because a pointer type > void* doesn't point to a void. And for bonus points a pointer of type > pointer-to-Base does not mean it doesn't point to a Derived. If we ignore types then we can instead talk about objects and sub-objects. A pointer to an object is not the same as a pointer a sub-object of that object. > > On a scale of 1-100 your fail is about 90%. That is 10% of readers may > support your attempts to redefine programming terminology, the rest > will probably realise tha they never read so much pish in their lives. > :) Drivel. > > Next you will be trying to suggest that newbies don't learn about > arrays and instead should use std::vector or something ridiculous like > that. > Brainstorm.... You learned that way? :) I started using C++ in 1993 (having used C before that) and didn't start using STL containers until the second half of the 90s. When did you start using C++ exactly? /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-20 09:43 +1200 |
| Subject | Re: concatenate variable number of arguments and types to a char * |
| Message-ID | <9b83osFr4mU2@mid.individual.net> |
| In reply to | #9098 |
On 08/20/11 09:37 AM, Leigh Johnston wrote: > > I started using C++ in 1993 (having used C before that) and didn't start > using STL containers until the second half of the 90s. When did you > start using C++ exactly? When he started learning is irrelevant since he appears to lack the ability to comprehend. Do us all a favour: put him in your kill-file and get on with your life. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Paul <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-08-19 15:08 -0700 |
| Message-ID | <a4f06dae-5cc5-4c1c-99b9-76b12f63fc1c@g31g2000yqh.googlegroups.com> |
| In reply to | #9100 |
On Aug 19, 10:43 pm, Ian Collins <ian-n...@hotmail.com> wrote: > On 08/20/11 09:37 AM, Leigh Johnston wrote: > > > > > I started using C++ in 1993 (having used C before that) and didn't start > > using STL containers until the second half of the 90s. When did you > > start using C++ exactly? > > When he started learning is irrelevant since he appears to lack the > ability to comprehend. > > Do us all a favour: put him in your kill-file and get on with your life. > Are you skitzo?
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-08-19 16:14 -0400 |
| Message-ID | <j2mg6g$q84$1@dont-email.me> |
| In reply to | #9088 |
On 8/19/2011 3:18 PM, Juha Nieminen wrote: > Leigh Johnston<leigh@i42.co.uk> wrote: >> On 19/08/2011 15:31, Juha Nieminen wrote: >>> Leigh Johnston<leigh@i42.co.uk> wrote: >>>> char* can never point to an array; it can, however, point *into* an >>>> array or one past the end of an array. :) >>> >>> What's the difference? >>> >>> (I'd say that since you can delete/free a dynamically allocated array >>> via a pointer that points to its first element, the pointer in question >>> *is* pointing to the array.) >> >> Think of it this way: instead of an array of char lets have a vector of >> char: >> >> std::vector<char> v(1); >> char* p1 =&v[0]; // will compile >> char* p2 = v; // will not compile >> >> Clearly p1 is a pointer to a vector element not a pointer to a vector; >> p1 points to a vector element; it doesn't point to a vector; critically >> pointer decay does not occur as it does with the array case. > > I don't really understand. std::vector is not an array, it's a class. > It happens to internally manage an array, but it nevetheless isn't itself > one. For all we know it could be a class that prints the 99 bottles of > beer rhyme. > > char* isn't a class (especially not one of type std::vector), nor even > a pointer to one. > > Apples and oranges? Who cares? It is a pointless, time-wasting, discussion about terminology, and you've started picking on the form in which the arguments are delivered. Essentially this is "I say tom-AY-to, you say tom-AH-to". A pointer to an element from a sequence of T is not a pointer to an array of T because there is already a special type, a pointer to an array. For 'p' to be a pointer to an array of N T it would have to be declared T (*p)[N]. But we all say "pointer to an array of T" because it's easier than the unusual form of "pointer INTO an array of T", just like we say "a const reference" when we actually mean "a refernece to a const object". Now can we drop this? V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Paul <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-08-19 13:54 -0700 |
| Message-ID | <ecb07da0-6c37-4b3b-b17e-26d182c60817@r12g2000vbe.googlegroups.com> |
| In reply to | #9091 |
On Aug 19, 9:14 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote: > On 8/19/2011 3:18 PM, Juha Nieminen wrote: > > > > > > > Leigh Johnston<le...@i42.co.uk> wrote: > >> On 19/08/2011 15:31, Juha Nieminen wrote: > >>> Leigh Johnston<le...@i42.co.uk> wrote: > >>>> char* can never point to an array; it can, however, point *into* an > >>>> array or one past the end of an array. :) > > >>> What's the difference? > > >>> (I'd say that since you can delete/free a dynamically allocated array > >>> via a pointer that points to its first element, the pointer in question > >>> *is* pointing to the array.) > > >> Think of it this way: instead of an array of char lets have a vector of > >> char: > > >> std::vector<char> v(1); > >> char* p1 =&v[0]; // will compile > >> char* p2 = v; // will not compile > > >> Clearly p1 is a pointer to a vector element not a pointer to a vector; > >> p1 points to a vector element; it doesn't point to a vector; critically > >> pointer decay does not occur as it does with the array case. > > > I don't really understand. std::vector is not an array, it's a class. > > It happens to internally manage an array, but it nevetheless isn't itself > > one. For all we know it could be a class that prints the 99 bottles of > > beer rhyme. > > > char* isn't a class (especially not one of type std::vector), nor even > > a pointer to one. > > > Apples and oranges? > > Who cares? It is a pointless, time-wasting, discussion about > terminology, and you've started picking on the form in which the > arguments are delivered. Essentially this is "I say tom-AY-to, you say > tom-AH-to". A pointer to an element from a sequence of T is not a > pointer to an array of T because there is already a special type, a > pointer to an array. For 'p' to be a pointer to an array of N T it > would have to be declared T (*p)[N]. But we all say "pointer to an > array of T" because it's easier than the unusual form of "pointer INTO > an array of T", just like we say "a const reference" when we actually > mean "a refernece to a const object". > You got that wrong. We say it's a pointer to an array because it is a pointer to an array, not because its some sloppy terminology. A pointer INTO an array would be a pointer that points to a sub-array. A pointer INTO the array could not be considered as a pointer to either the first element nor one past the end whereas a pointer to an array can , and generally is in the case of the first element.
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-08-19 22:01 +0100 |
| Message-ID | <Dr2dnQmT9fGxUtPTnZ2dnUVZ7oudnZ2d@giganews.com> |
| In reply to | #9092 |
On 19/08/2011 21:54, Paul wrote:
> On Aug 19, 9:14 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> On 8/19/2011 3:18 PM, Juha Nieminen wrote:
>>
>>
>>
>>
>>
>>> Leigh Johnston<le...@i42.co.uk> wrote:
>>>> On 19/08/2011 15:31, Juha Nieminen wrote:
>>>>> Leigh Johnston<le...@i42.co.uk> wrote:
>>>>>> char* can never point to an array; it can, however, point *into* an
>>>>>> array or one past the end of an array. :)
>>
>>>>> What's the difference?
>>
>>>>> (I'd say that since you can delete/free a dynamically allocated array
>>>>> via a pointer that points to its first element, the pointer in question
>>>>> *is* pointing to the array.)
>>
>>>> Think of it this way: instead of an array of char lets have a vector of
>>>> char:
>>
>>>> std::vector<char> v(1);
>>>> char* p1 =&v[0]; // will compile
>>>> char* p2 = v; // will not compile
>>
>>>> Clearly p1 is a pointer to a vector element not a pointer to a vector;
>>>> p1 points to a vector element; it doesn't point to a vector; critically
>>>> pointer decay does not occur as it does with the array case.
>>
>>> I don't really understand. std::vector is not an array, it's a class.
>>> It happens to internally manage an array, but it nevetheless isn't itself
>>> one. For all we know it could be a class that prints the 99 bottles of
>>> beer rhyme.
>>
>>> char* isn't a class (especially not one of type std::vector), nor even
>>> a pointer to one.
>>
>>> Apples and oranges?
>>
>> Who cares? It is a pointless, time-wasting, discussion about
>> terminology, and you've started picking on the form in which the
>> arguments are delivered. Essentially this is "I say tom-AY-to, you say
>> tom-AH-to". A pointer to an element from a sequence of T is not a
>> pointer to an array of T because there is already a special type, a
>> pointer to an array. For 'p' to be a pointer to an array of N T it
>> would have to be declared T (*p)[N]. But we all say "pointer to an
>> array of T" because it's easier than the unusual form of "pointer INTO
>> an array of T", just like we say "a const reference" when we actually
>> mean "a refernece to a const object".
>>
> You got that wrong.
> We say it's a pointer to an array because it is a pointer to an array,
> not because its some sloppy terminology.
> A pointer INTO an array would be a pointer that points to a sub-array.
> A pointer INTO the array could not be considered as a pointer to
> either the first element nor one past the end whereas a pointer to an
> array can , and generally is in the case of the first element.
Actually Victor got it right; perfectly. What you just uttered was
simply noise.
If you want a pointer to an array then use a pointer to an array:
typedef int array_type[42];
array_type* pointer_to_an_array;
int a[42];
pointer_to_an_array = &a;
HTH.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Paul <pchristor@yahoo.co.uk> |
|---|---|
| Date | 2011-08-19 14:20 -0700 |
| Message-ID | <3676bff8-23eb-4982-8d26-4525c7d8c739@k3g2000vbz.googlegroups.com> |
| In reply to | #9093 |
On Aug 19, 10:01 pm, Leigh Johnston <le...@i42.co.uk> wrote: > On 19/08/2011 21:54, Paul wrote: > > > > > > > On Aug 19, 9:14 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote: > >> On 8/19/2011 3:18 PM, Juha Nieminen wrote: > > >>> Leigh Johnston<le...@i42.co.uk> wrote: > >>>> On 19/08/2011 15:31, Juha Nieminen wrote: > >>>>> Leigh Johnston<le...@i42.co.uk> wrote: > >>>>>> char* can never point to an array; it can, however, point *into* an > >>>>>> array or one past the end of an array. :) > > >>>>> What's the difference? > > >>>>> (I'd say that since you can delete/free a dynamically allocated array > >>>>> via a pointer that points to its first element, the pointer in question > >>>>> *is* pointing to the array.) > > >>>> Think of it this way: instead of an array of char lets have a vector of > >>>> char: > > >>>> std::vector<char> v(1); > >>>> char* p1 =&v[0]; // will compile > >>>> char* p2 = v; // will not compile > > >>>> Clearly p1 is a pointer to a vector element not a pointer to a vector; > >>>> p1 points to a vector element; it doesn't point to a vector; critically > >>>> pointer decay does not occur as it does with the array case. > > >>> I don't really understand. std::vector is not an array, it's a class. > >>> It happens to internally manage an array, but it nevetheless isn't itself > >>> one. For all we know it could be a class that prints the 99 bottles of > >>> beer rhyme. > > >>> char* isn't a class (especially not one of type std::vector), nor even > >>> a pointer to one. > > >>> Apples and oranges? > > >> Who cares? It is a pointless, time-wasting, discussion about > >> terminology, and you've started picking on the form in which the > >> arguments are delivered. Essentially this is "I say tom-AY-to, you say > >> tom-AH-to". A pointer to an element from a sequence of T is not a > >> pointer to an array of T because there is already a special type, a > >> pointer to an array. For 'p' to be a pointer to an array of N T it > >> would have to be declared T (*p)[N]. But we all say "pointer to an > >> array of T" because it's easier than the unusual form of "pointer INTO > >> an array of T", just like we say "a const reference" when we actually > >> mean "a refernece to a const object". > > > You got that wrong. > > We say it's a pointer to an array because it is a pointer to an array, > > not because its some sloppy terminology. > > A pointer INTO an array would be a pointer that points to a sub-array. > > A pointer INTO the array could not be considered as a pointer to > > either the first element nor one past the end whereas a pointer to an > > array can , and generally is in the case of the first element. > > Actually Victor got it right; perfectly. What you just uttered was > simply noise. > > If you want a pointer to an array then use a pointer to an array: > > typedef int array_type[42]; > array_type* pointer_to_an_array; > int a[42]; > pointer_to_an_array = &a; > > HTH. > > /Leigh- Hide quoted text - > > - Show quoted text -
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web