Click here to Skip to main content
15,886,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to get the client's abort message with WSAAsyncSelect ? Pin
Member 131329513-Jul-11 18:32
Member 131329513-Jul-11 18:32 
AnswerRe: How to get the client's abort message with WSAAsyncSelect ? Pin
includeh1014-Jul-11 9:44
includeh1014-Jul-11 9:44 
GeneralRe: How to get the client's abort message with WSAAsyncSelect ? Pin
wangningyu14-Jul-11 15:25
wangningyu14-Jul-11 15:25 
Question[Solved] Passing an element from a list [modified] Pin
crgmustang13-Jul-11 13:50
crgmustang13-Jul-11 13:50 
AnswerRe: Passing an element from a list Pin
«_Superman_»13-Jul-11 14:48
professional«_Superman_»13-Jul-11 14:48 
GeneralRe: Passing an element from a list Pin
crgmustang14-Jul-11 5:28
crgmustang14-Jul-11 5:28 
QuestionPointer Maths Pin
KeithF12-Jul-11 3:25
KeithF12-Jul-11 3:25 
AnswerRe: Pointer Maths [modified] Pin
Stefan_Lang12-Jul-11 3:52
Stefan_Lang12-Jul-11 3:52 
Adding or subtracting from a pointer will always be done in multiples of the size of the type pointed to. In this case, MoveBack subtracts 12 times the size of an int from the address, since i points to an int (or at least that's how you declared it). If you don't want that, you have to use void* rather than int*.

That said, it's a horrible idea to cast a pointer into an array of some struct to int*! Why don't you just pass a pointer to MyStruct instead? Also, it's an even worse idea to dereference an int pointer that in truth points to a struct. This may or may not work, depending on the compiler. And if you ever choose to modify your struct later, the code will likely break (provided it did actually work before).

MyStruct* MoveBack(const MyStruct* p)
{
   return p-1; // moves pointer to previous struct in array
}

// to use it do this:
   printf("The Last Record is : %ld" , MoveBack(&ts[i])->k);

P.S.: Note that this solution
a) makes the function independent of the definition of MyStruct
b) gets rid of error prone type casts
c) removes the need to use sizeof
d) is actually shorter, to the point that you could inline it, or simply forgo the function alltogether (e. g., above you could write (&ts[i]-1)->k)

P.P.S.: you could of course just use ts[i-1].k
That would be technically equivalent to the code I suggested under d) above

modified on Tuesday, July 12, 2011 9:59 AM

GeneralRe: Pointer Maths Pin
Legor12-Jul-11 4:02
Legor12-Jul-11 4:02 
GeneralRe: Pointer Maths Pin
Stefan_Lang12-Jul-11 4:16
Stefan_Lang12-Jul-11 4:16 
AnswerRe: Pointer Maths Pin
Legor12-Jul-11 3:55
Legor12-Jul-11 3:55 
GeneralRe: Pointer Maths Pin
Rick York13-Jul-11 18:39
mveRick York13-Jul-11 18:39 
GeneralRe: Pointer Maths Pin
Legor17-Jul-11 0:39
Legor17-Jul-11 0:39 
GeneralRe: Pointer Maths Pin
enhzflep13-Jul-11 19:14
enhzflep13-Jul-11 19:14 
GeneralRe: Pointer Maths Pin
Richard MacCutchan13-Jul-11 21:53
mveRichard MacCutchan13-Jul-11 21:53 
QuestionHow to do 2 or more process simultaneously? Pin
Le@rner12-Jul-11 2:21
Le@rner12-Jul-11 2:21 
QuestionRe: How to do 2 or more process simultaneously? Pin
CPallini12-Jul-11 3:02
mveCPallini12-Jul-11 3:02 
AnswerRe: How to do 2 or more process simultaneously? Pin
Le@rner12-Jul-11 18:26
Le@rner12-Jul-11 18:26 
GeneralRe: How to do 2 or more process simultaneously? [fixed] Pin
CPallini12-Jul-11 21:09
mveCPallini12-Jul-11 21:09 
GeneralRe: How to do 2 or more process simultaneously? Pin
Le@rner12-Jul-11 21:16
Le@rner12-Jul-11 21:16 
GeneralRe: How to do 2 or more process simultaneously? Pin
CPallini12-Jul-11 21:24
mveCPallini12-Jul-11 21:24 
AnswerRe: How to do 2 or more process simultaneously? Pin
Albert Holguin12-Jul-11 4:56
professionalAlbert Holguin12-Jul-11 4:56 
AnswerRe: How to do 2 or more process simultaneously? Pin
jschell12-Jul-11 10:21
jschell12-Jul-11 10:21 
GeneralRe: How to do 2 or more process simultaneously? Pin
Le@rner12-Jul-11 19:54
Le@rner12-Jul-11 19:54 
GeneralRe: How to do 2 or more process simultaneously? Pin
Chris Walters13-Jul-11 6:24
Chris Walters13-Jul-11 6:24 

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.