Hi all...my first post here...WOOHOO!! Ok...now to the meat of the question...
I'm a student at the the local JC in an intermediate C++ class...I'm having trouble understanding pointers...I just can't seem to get the full concept through my head...Can someone tell me how they finally "got it"? I understand (I think) the "->" operator, but I still don't understand exactly what it means if I've got a function header with a pointer as one of the arg's. ie

foo::somefunc(char *somevar)

what does that mean?????
If i've got some variable called

char *word[6]

do I call

somefunc(word)

or do I call

somefunc(*word)

What exactly is the difference?? And how do I refer to the variable w/in the somefunc function??
I'm soooo confused right now, and would appreciate any *pointers :cheesy: * you could give me....thanx!!

Dani AI

Generated

A few practical points that build on and : the single most useful mental model is to watch types, not just symbols. A function parameter declared to take a pointer-to-char expects an address (the start of a character sequence). Whether a name is an array, an array of pointers, or a pointer changes what the bare name decays to when used in a call.

Concrete examples you can compile and try:

void show(const char* s) {
    // inside: s[0] or *s is the first character, s[i] indexes, ++s advances
}

char buf[] = "hello";
show(buf);      // OK: buf decays to const char*
show(*buf);     // WRONG: *buf is a char (first character), not a pointer

And for an array of pointers:

const char* list[] = { "one", "two" };
show(list[0]);  // OK: element is const char* (a string)
show(list);     // WRONG: list decays to const char** (pointer-to-pointer)
show(*list);    // OK: *list == list[0]

Quick rules and tips: array-name usually decays to pointer-to-first-element; *name gives the element; for char[] that element is char, for char*[] the element is char*. Do not try to modify string literals — treat them as const char[]. Use compiler warnings to catch type mismatches, prefer const where appropriate, and in C++ prefer std::string / std::vector<std::string> to avoid raw-pointer pitfalls. Practicing small examples like the ones above will make the concepts click quickly.

Recommended Answers

All 3 Replies

>>Hi all...my first post here...WOOHOO!! Ok...now to the meat of the question

Welsome to DaniWeb.:) Hope we see a lot more of you here and there. Please visit the Coffee House --> Community Introductons and tell us about yourself.

maybe you should read one of the you can find on the net.

>>Can someone tell me how they finally "got it"?
through lots and lots of practice and experimenting.

>>What exactly is the difference
The difference is passing a single character or the address of the first element in the array. The star passes the first character in the array, while without the star passes the address of the first character.

>>What exactly is the difference?
Apart from reading up on the material given by AD above, just one note, remember that that "*" in somefunc(*word) is an operator. So try and figure out what the * operator does. The other operator of interest is "&" which does exactly the opposite of what "*" does. Understanding these 2 (on top of ->) would help immensely.

hi all...thanks for the tips...I guess I'll just have to play around with them for awhile until I'm proficient...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.