#Putting a pointer into a struct makes array unreachable

21 messages · Page 1 of 1 (latest)

feral lagoonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

shadow elm
#

relevant output:

Centaur
C```
#

Also tried this because i know you can sometimes sorta increment pointers to get to their next slot but it didn't work:

    cout << char_array << endl; //Works
    cout << *(patient_list[5].name+1) << endl; //Doesn't work```
output:

Centaur
//Blank line```

burnt island
#

Can you show a complete example? You don't even show the struct you're talking about.

shadow elm
#

my bad. here's the struct. is there anything else im not including here?

struct patient {
    int age;
    char name[20];
    float balance;
};```
runic frigate
stiff fern
#

So the first thing that strikes me is that you actually only store one char into your struct.
I'm not even sure how it compiles.

I think you need to understand what * does and when to use it

#

So, first use: multiplication, obvious

#

Second use: using to declare/create a pointer (or to mark a pointer type)

When you do:

const char* text you declare text to be a pointer * to char. If it's an array, the pointer points to the first element of the array.

#

text[1] access the "next" object in the array after the first one, which is text[0] or alternatively *text

#

So we get to the third use:

Dereference

#

*ptr gets the value from beneath the pointer. *char_array gets the value that the pointer points to, so just the first character. Same happens later in your cout

#

Ah right - (almost) whenever an array is used in an expression, it "decays" to a pointer to its first element

#

In the case of cout, if you drop the * it will just work, the << operator expects const char* as input

#

For initializing your struct, it's not that obvious, since the arrays are not assignable, you can't just copy them or assign one to the other (which is a language limitation). For copying C strings (in char arrays) you use a special C function: strcpy. It is relatively dangerous

#

||You might be inclined to use strcpy_s from C11... Don't. It's not portable, and it doesn't really increase the security much over the existing tools.||

#

It seems that the coffee you are creating is a weird amalgamation of C and C++. This style is objectively wrong. If you have access to an abstraction like std::string, use it to store the strings everywhere, do not switch between std:: string and char buffers

#

geeksforgeeks has extremely poor quality articles. You will find many bad practices there, as well as straight up wrong information

feral lagoonBOT
#

This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

shadow elm
#

!solved

feral lagoonBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity