Archived

This topic is now archived and is closed to further replies.

vector and sscanf

This topic is 5896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  
// C code:

float f[20]; // possibly uninitialized floats

sscanf ("%f", &f[10]); // scan into the 11th element


// STL code:

vector<float> f (20); // initializes vector to 0.0

sscanf ("%f", &f[10]); // scan into the 11th element



Gotta love STL.

Share this post


Link to post
Share on other sites
Well sscanf needs a pointer so it should probably be:
    
vector<float> f (20); // initializes vector to 0.0

sscanf ("%f", &(*f[10])); // scan into the 11th element


But I honestly have no idea if that will work.

Share this post


Link to post
Share on other sites
quote:
Original post by Scarab0
Well sscanf needs a pointer so it should probably be:

In many contexts there is no effective difference between references and pointers. Further more, the fact that a function takes a pointer as an argument doesn''t mean you must pass it an argument; it means the function will take the address of whatever argument you pass to it (with type restrictions). ie

int func( int *pInteger );
int i = 255;
func(&i); // note need to get address-of i using operator &.
int j[25];
// initialize j if necessary
func(j); // still valid; func() takes address of j[].
int *k;
k = new int[NUM_INTS]; // assume NUM_INTS to be a constant
func(k); // same as func(j), only array is dynamic. func() still takes address of k[].

Share this post


Link to post
Share on other sites
Just for the record, the proper sscanf statement is

  
sscanf("%f", &f[10]);


pacrugby:
What you''re trying to do is impossible. Period. The others have made very good explanations why.

Share this post


Link to post
Share on other sites