Click here to Skip to main content
15,895,011 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Extract RGB Colors Pin
Eugen Podsypalnikov13-Dec-13 3:18
Eugen Podsypalnikov13-Dec-13 3:18 
QuestionC pointer and array question [Resolved] Pin
econy12-Dec-13 20:07
econy12-Dec-13 20:07 
AnswerRe: C pointer and array question Pin
econy12-Dec-13 20:19
econy12-Dec-13 20:19 
AnswerRe: C pointer and array question [Resolved] Pin
Richard MacCutchan12-Dec-13 21:19
mveRichard MacCutchan12-Dec-13 21:19 
AnswerRe: C pointer and array question [Resolved] Pin
Stefan_Lang12-Dec-13 22:42
Stefan_Lang12-Dec-13 22:42 
GeneralRe: C pointer and array question [Resolved] Pin
econy16-Dec-13 9:14
econy16-Dec-13 9:14 
GeneralRe: C pointer and array question [Resolved] Pin
Stefan_Lang16-Dec-13 21:25
Stefan_Lang16-Dec-13 21:25 
AnswerRe: C pointer and array question [Resolved] Pin
Stefan_Lang12-Dec-13 23:14
Stefan_Lang12-Dec-13 23:14 
econy wrote:
then I can get ts1.c1 = 3, ts1.c2 = 4, ts1.c3 = 5,..

Note that there is no guarantee the compiler will lay out struct members in the exact same order you defined them.

econy wrote:
ts1 = *(TestSTruct*)(&s1[2]);

There is no guarantee this will work as expected with any modern compiler, especially if that struct contains more than just four chars. The compiler may have to modify the total memory layout depending on padding, virtual function table, base classes, and debug information (and possibly for other reasons, too).

If you want a clean solution, just provide a simple constructor and maybe assignment operator:
C++
struct TestStruct {
   TestStruct(const char c1_, const char c2_, const char c3_, const char c4_)
    : c1(c1_), c2(c2_), c3(c3_), c4(c4_)
   {
   }
   TestStruct(const char* s)
   {
      operator=(*s);
   }
   TestStruct* operator=(const char* s)
   {
      if (s != nullptr)
      {
         c1 = c1_;
         c2 = c2_;
         c3 = c3_;
         c4 = c4_;
      }
      return *this;
   }

   ...
};

You can then initialize your structs in any of the following ways:
C++
const char* s1 = {1, 2, 3, 4, 5, 6, 7};
struct TestStruct ts1(3, 4, 5, 6);
struct TestStruct ts2(s1+2);
struct TestStruct ts3;
ts3 = s1+2;

Please note that this last assignment may lead to unintentional errors if used carelessly. but at least it's readable and extendable.

[edit]
Sorry, I just noted that you asked about C, not C++. For C, most of my concerns expressed above don't apply! Your assignment statement is essentially correct in that case (although you could simplify &s1[2] to s1+2 if you want to)
[/edit]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)


modified 13-Dec-13 5:46am.

QuestionReplacement for Inline Assembly Pin
Richard Andrew x6412-Dec-13 18:08
professionalRichard Andrew x6412-Dec-13 18:08 
QuestionRe: Replacement for Inline Assembly Pin
Richard MacCutchan12-Dec-13 21:05
mveRichard MacCutchan12-Dec-13 21:05 
AnswerRe: Replacement for Inline Assembly Pin
Richard Andrew x6413-Dec-13 7:44
professionalRichard Andrew x6413-Dec-13 7:44 
AnswerRe: Replacement for Inline Assembly Pin
Rajesh R Subramanian12-Dec-13 23:31
professionalRajesh R Subramanian12-Dec-13 23:31 
AnswerRe: Replacement for Inline Assembly PinPopular
Eugen Podsypalnikov13-Dec-13 1:14
Eugen Podsypalnikov13-Dec-13 1:14 
GeneralRe: Replacement for Inline Assembly Pin
Richard Andrew x6413-Dec-13 7:25
professionalRichard Andrew x6413-Dec-13 7:25 
GeneralRe: Replacement for Inline Assembly Pin
CPallini13-Dec-13 10:02
mveCPallini13-Dec-13 10:02 
AnswerRe: Replacement for Inline Assembly Pin
Chris Losinger15-Dec-13 4:47
professionalChris Losinger15-Dec-13 4:47 
GeneralRe: Replacement for Inline Assembly Pin
Richard Andrew x6415-Dec-13 5:20
professionalRichard Andrew x6415-Dec-13 5:20 
QuestionC Language Tutorial Pin
tgsb11-Dec-13 22:00
tgsb11-Dec-13 22:00 
AnswerRe: C Language Tutorial Pin
Richard MacCutchan11-Dec-13 22:07
mveRichard MacCutchan11-Dec-13 22:07 
QuestionHow to get the android nowtime screen-picture from PC by adb ?? Pin
chehongyang11-Dec-13 19:41
chehongyang11-Dec-13 19:41 
AnswerRe: How to get the android nowtime screen-picture from PC by adb ?? Pin
Albert Holguin12-Dec-13 4:16
professionalAlbert Holguin12-Dec-13 4:16 
Questionto show an color filled object on the top of everything Pin
transoft11-Dec-13 8:34
transoft11-Dec-13 8:34 
AnswerRe: to show an color filled object on the top of everything Pin
Chris Losinger11-Dec-13 10:39
professionalChris Losinger11-Dec-13 10:39 
AnswerRe: to show an color filled object on the top of everything Pin
Albert Holguin12-Dec-13 4:19
professionalAlbert Holguin12-Dec-13 4:19 
Questionstd::swap Corruption [Answered] Pin
Skippums11-Dec-13 7:26
Skippums11-Dec-13 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.