Re: passing a string to a C++ function

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



The first topic? Already did it on my own. I had an 18 year old give me a
typical teenaged attitude answer.

But arguing about this is a waste of time.

The thing is that anything I read is basic. Most array stuff I read doesn't
go beyong and array of integers.

Which is why I am asking for specific help

I certainly do kno wwhat I want my function to do, I just left parts that
are irrelevant out of the function

All I said is that I needed to pas a string to this function in two ways:

get it through a text file in the form of:

"item1"
"item2"
"item3"
"item4"
........

have these assigned to a string ( or passed to the function if possible) in
this form:

char *items[]={"item1","item2","item3","item4"...)

put those in a loop and print them out one by one

You are right I should revisit pointers since it's been about 7 years since
I last had to work with them at all, but my time is limited and I thought I
could get a bit of a shortcut by having someone probbaly effortlesly provide
me with an example of that type of function

No disrespect meant. I have helped many people in other programming
languages in my life, and written a lot of free code. Now I am in an area
that I basically understand but am far from good at it, and that is VC++








"David Webber" <dave@xxxxxxxxxxxxxxxxxxx> wrote in message
news:e4V9zcdyFHA.3320@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Leo" <none@xxxxxxxx> wrote in message
> news:eBSdy8cyFHA.3892@xxxxxxxxxxxxxxxxxxxxxxx
>
> > Hmm...thanks for a very unhelpful answer.
>
> If you explore its consequences you will not find it unhelpful.
>
> > let me rephrase it:
> >
> > char items[3]={"tom","dick","harry"}
> > does that satisfy your "remaining 3 pointers fixation"?
>
> I'm afraid I have a "what the hell is this supposed to mean"
> fixation and I believe the compiler will have too.
>
> > now onto the next:
> > Forget the above,
>
> Right ho.
>
> > if I have a string with "tom","dick","harry"
> > can I build an array with them?
>
> Three strings. Yes you can build an array with them....
>
> > (I will learn more about pointers when I have some time.
>
> ...just as soon as you have learned more about pointers.
>
> > For now all I need
> > is a very simple function. Either you can help me with some code,
> > or leave
> > the preaching for another time. Sorry to be so blunt, but I am
> > getting all
> > kinds of answers, yet none related to what I am asking for).
>
> Equally désolé, but it is vary far from clear what you are asking
> for. You have been advised by others to use CString or std::string
> and std:vector but you have rejected these. The *reason* you have
> been advised this is that it is possible (but probably not
> advisable) to use them without knowing much about pointers and
> memory management. You can't use standatd nul-terminated strings
> without knowing about pointers, and about how to handle the memory
> they point to. You can't even specify what you want the function
> to do without understanding this, so even if this group were a
> collection of people volunterring to write code for free, thsey
> couldn't do it because you can't specify it. People have been very
> helpful on both threads you have started on this topic. All the
> answers make perfect sense, and (to be equally blunt) all are trying
> to work out what the hell you are asking for! Perhaps when you
> have had time to work out exactly what the question is, you'll
> understand the answers a bit better.
>
> Dave
> --
> David Webber
> Author MOZART the music processor for Windows -
> http://www.mozart.co.uk
> For discussion/support see
> http://www.mozart.co.uk/mzusers/mailinglist.htm
>
>
>
>
>
>
> >
> >
> >
> >
> > "David Webber" <dave@xxxxxxxxxxxxxxxxxxx> wrote in message
> > news:uXx2idcyFHA.3812@xxxxxxxxxxxxxxxxxxxxxxx
> >>
> >> "Leo" <none@xxxxxxxx> wrote in message
> >> news:%23tT%23VRcyFHA.596@xxxxxxxxxxxxxxxxxxxxxxx
> >>
> >> > 'items' is not an array pointer. It's a pointer array.
> >> > -- Ok!
> >> >
> >> > my point was not how I am DECLARING the array. My question was:
> >> >
> >> > passing a bunch of items to the function that will put them
> >> > into
> >> > this array.
> >>
> >> You can't. You really *do* need to learn what pointers are.
> >>
> >> The statement
> >>
> >> char *items[6]={"tom","paul","joseph"};
> >>
> >> declares six pointers each to a char. It also initialises some
> >> memory with the strings and sets the first three pointers to
> >> point
> >> to them. The rmaining three pointers are just pointers that
> >> point
> >> nowhere in particular.
> >>
> >> > so passing item1, item2, item3 and store them into the items[]
> >> > array.
> >>
> >> You can't store anything in the items array except pointers.
> >> And
> >> when you leave the function they will go out of scope, and be
> >> lost,
> >>
> >> >...
> >> > Any thoughts?
> >>
> >> Yes. Learn C or C++. Learn what a pointer is, and what an
> >> array
> >> is, and their relationship. Think about what is stored in
> >> memory
> >> where, and how you get access to it, rather than creating invalid
> >> pointers. I'm sorry but you have to do this - there is no
> >> shortcut.
> >>
> >> Dave
> >> --
> >> David Webber
> >> Author MOZART the music processor for Windows -
> >> http://www.mozart.co.uk
> >> For discussion/support see
> >> http://www.mozart.co.uk/mzusers/mailinglist.htm
> >>
> >>
> >>
> >
> >
>
>


.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)
  • Re: OK, Im stumped, couldnt free string pointer list...
    ... by alpha order of "substrings" at certain locations in the strings. ... the list of string pointers! ... If you write outside the area of a memory you allocated ...
    (comp.lang.c)
  • Re: Dusty Deck and C memory manager, Part 1
    ... CMEMC.C -- C memory management routines for use with FORTRAN. ... Return an index into ARRAY representing LENGTH words of available ... void *ptr, *malloc; ... Rather than introduce Cray pointers and use C routines to allocate memory for Fortran arrays, you could use straight F90, for example: ...
    (comp.lang.fortran)
  • Re: Array of Pointers
    ... > If you are repeatedly searching for strings within a large file ... Needles are usually smaller than haystacks. ... It is known that the file is likely already in memory cache. ... strings and set the pointers, which will be used later to perform the ...
    (comp.lang.c)
  • Re: Simple C containers, std::vector analog
    ... You're right about not needing to care very much about the memory ... blocks, in not knowing the ultimate size of the growable array, because ... there is no realloc function, and besides realloc might be having ... whether it's used for the list of array block pointers only or a C++ ...
    (comp.lang.c)