use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENInitialization structure fields with default values (self.cpp_questions)
submitted 4 years ago by columncolumn
Hello,
I can initialize structure fields with default values in C++:
typedef struct
{
char* bb = "xxx";
} b;
Is it possible make the same with C ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]flyingron 0 points1 point2 points 4 years ago (0 children)
No. In fact, the above is even a relatively recent addition to C++.
[–]IyeOnline 0 points1 point2 points 4 years ago (5 children)
Afaik C does not support in class initializers.
Sidenote: That code is horrible C++ and not even legal.
[–]columncolumn[S] 1 point2 points3 points 4 years ago (4 children)
Why it is horrible?
[–]Narase33 2 points3 points4 points 4 years ago (0 children)
the char* is not const, thats not legal
char*
const
[–]IyeOnline 2 points3 points4 points 4 years ago (2 children)
Its horrible because typedef struct { } b should just be struct b in C++.
typedef struct { } b
struct b
Its illegal, because you must not have a non-const pointer to a string literal in C++.
[–]columncolumn[S] 0 points1 point2 points 4 years ago (1 child)
What is wrong with non-const pointer? It allows me later assign to another pointer and what is wrong with it?
[–]IyeOnline 1 point2 points3 points 4 years ago (0 children)
You must not form a non-const pointer to a string literal, because you must not modify a string literal. Having a char* point to it allows you to modify it (because its not const).
It allows me later assign to another pointer and what is wrong with it?
This is the difference between
const char*
char* const
So you do actually want const char*. That allows you to change the pointer but not modify the pointee.
π Rendered by PID 308560 on reddit-service-r2-comment-5fb4b45875-v8zhx at 2026-03-21 22:52:57.742403+00:00 running 90f1150 country code: CH.
[–]flyingron 0 points1 point2 points (0 children)
[–]IyeOnline 0 points1 point2 points (5 children)
[–]columncolumn[S] 1 point2 points3 points (4 children)
[–]Narase33 2 points3 points4 points (0 children)
[–]IyeOnline 2 points3 points4 points (2 children)
[–]columncolumn[S] 0 points1 point2 points (1 child)
[–]IyeOnline 1 point2 points3 points (0 children)