typedef struct + assignments

Programming, for all ages and all languages.
Post Reply
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

typedef struct + assignments

Post by Troy Martin »

Hey all,

I was wondering if anyone knew how to do something like this:

Code: Select all

typedef struct
{
    int length = strlen(this); /* get the length of the string pointed to by the variable declared here */
} *string_sz;

string_sz str = "Hello World!\n";
So str is a pointer to a C string and length is the string's length.

Will that work or do I have some modifications to make to get it to work like a string from the C++ string library?
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: typedef struct + assignments

Post by JohnnyTheDon »

No, it won't work :( . You need a bunch of functions in a class to do this kind of thing.

Code: Select all

class sz_string
{
public:
    String(const char* str="") { mString = str; mLength = strlen(str); }
    operator const char*() const { return mString; }
    int Length() const { return mLength; }
protected:
    const char* mString;
    int mLength;
}
If you're trying to do this in C, AFAIK it isn't possible.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: typedef struct + assignments

Post by Creature »

What JohnnyTheDon says. You can pretty much not do that in C, because C doesn't support (as far as I know) nor operator overloading, nor functions in structures.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: typedef struct + assignments

Post by 01000101 »

AFAIK you're stuck with using plain ol' char arrays and using strlen(x). =)
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: typedef struct + assignments

Post by Troy Martin »

Yuck, this sucks.

Oh well, thanks anyways!
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: typedef struct + assignments

Post by JamesM »

Think about it - if you change the value of the char* member, how would the length member update? C has no runtime support system.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: typedef struct + assignments

Post by Love4Boobies »

JamesM made the same point I wanted to make but here's another - WHY would this ever be needed?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: typedef struct + assignments

Post by JohnnyTheDon »

Getting the length of a string with running strlen every time.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: typedef struct + assignments

Post by Love4Boobies »

Well it's not stored anywhere. If you want this to happen, just make sure you update a variable with the length of the string each time you change it.

EDIT: Or you Pascal :)
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: typedef struct + assignments

Post by Combuster »

or link to the freebasic runtime (written in C :) ) and use that string format + functions

In the end, it all boils down to the same thing...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: typedef struct + assignments

Post by Love4Boobies »

Combuster wrote:In the end, it all boils down to the same thing...

Boobies?

Sorry, couldn't help it...
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: typedef struct + assignments

Post by Combuster »

Also an option, but it only circumvents the problem by not having to write code :twisted:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply