Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
FastEddie
Oct 4, 2003

Remulak posted:

You're right. They're what's called a logical shift. If it carried the bit around it would be a rotate.
No, they're bitwise shifts, because they operate on bits, rather than the boolean interpretation of the collections of bits. I have no idea what a logical shift would be.

Adbot
ADBOT LOVES YOU

FastEddie
Oct 4, 2003

CliffyBMine! posted:


code:
void reverse_string(char *sz) {
  char *e;
  if (!sz[0]) return;
  for (e = sz; e[1]; ++e);
  for (; sz < e; ++sz, --e) {
    char t = *e;
    *e = *sz;
    *sz = t;
  }
}
And here's a test:
code:
#include <stdio.h>
#include <string.h>

int errors = 0;

void test(const char *in, const char *expected) {
  char *in_copy = strdup(in);
  reverse_string(in_copy);
  if (strcmp(expected, in_copy)) {
    printf("FAIL - expected %s, got %s\n", expected, in_copy);
    ++errors;
  }
  free(in_copy);
}

int main() {
  test("", "");
  test("a", "a");
  test("abc", "cba");
  test("Hello,  World  !", "!  dlroW  ,olleH");
  if (!errors) puts("PASS");
  return errors;
}

FastEddie
Oct 4, 2003

Avenging Dentist posted:

I hereby present FastEddie the Cavern of COBOL FizzBuzz Award for correctly solving the wrong problem!!!
Not if you put "olleh dneirf" into it. He wants to reverse the words in a sentence, no? And he's already reversed each word.

FastEddie
Oct 4, 2003

Avenging Dentist posted:

Yes, that is clearly what he meant and you're clearly not just backpedaling in an attempt to save face.
Why would he want to reverse the characters in each word? That's the stupidest interview question I've ever heard of!

FastEddie
Oct 4, 2003

irishcoffee posted:

I have a function definition as follows:

void foo(void *arr, size_t num_elem, size_t elem_size)

Arr can point to any array of strings, ints, longs, floats, whatever.

How would I go about memcpying this array regardless of type
code:
void *destination = ...
memcpy(destination, arr, num_elem * elem_size);

quote:

and referencing its elements, regardless of type.
code:
void *elem = (char*)arr + (index * elem_size);
You'll need another cast if you're using C++.

FastEddie fucked around with this message at 21:01 on Dec 9, 2009

FastEddie
Oct 4, 2003

Awesome Andrew posted:

Is it possible to pass a single (and not predefined) element of a structure to a parameter?

For instance:

code:
struct Stats {
	int health, mana;
	int strength, vitality, agility;
	int intelligence, willpower;
};
code:
class Character : GameObject {
...

Stats base_stats;

public:
...

void change_stat(std::string stat_name, int magnitude) {
	base_stats.stat_name =+ magnitude;
}

...
};
Or do I just need to create a separate function for each element?
You could use an array of integers in the struct, and then have an enum for indexing into the array.
code:
enum StatType {
  HEALTH, MANA, VITALITY, ETC,
  NUM_STATS;
};
struct Stats {
  int value[NUM_STATS];
};
Then instead of passing a reference or pointer, you'd pass the enum for the stat you wanted to update.
code:
void change_stat(StatType stat, int magnitude) {
  base_stats.value[stat] += magnitude;
}
And then you'd have something like this:
code:
obj.change_stat(HEALTH, 10);

Adbot
ADBOT LOVES YOU

FastEddie
Oct 4, 2003

The downside to adding items to the end of a vector is that the internal array may be resized when its capacity is reached. This can be a somewhat expensive operation if your vector already has a lot of items in it.

If you know in advance the number of items you'll be push_backing into your vector, you can make sure there's enough space in the internal array for them by calling the vector's "reserve" member function beforehand.

e: This is an answer to a question that I now cannot find in the thread. :confused:

FastEddie fucked around with this message at 10:59 on Mar 15, 2021

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply