Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

n00b question

Name: n00b 2009-01-26 23:37

Is there a way to declare an array in C so the indexing starts at something besides zero?  Like if I wanted the array to be a[-10] to a[10] instead of a[0] to a[20]?

Name: Anonymous 2009-01-26 23:42

Why would you want that?

Name: Anonymous 2009-01-26 23:44

I want an array that holds a number for each point in a 2D grid, but I want the origin (point (0,0)) to be in the middle, so I want to use negative indicies.

n/m though, I figured out how to do it with a #define.

Name: Anonymous 2009-01-26 23:47

I want an array that holds a number for each point in a 2D grid, but I want the origin (point (0,0)) to be in the middle, so I want to use negative indicies.

n/m though, I figured out how to do it with a #define.

Name: Anonymous 2009-01-26 23:47

>>4
Dammit...

Name: Anonymous 2009-01-27 0:09

USE A HASH

Name: Anonymous 2009-01-27 1:04

USE A HASKELL

Name: Anonymous 2009-01-27 1:24

>>4
#error hiss preprocessor

std::tr1::unordered_map allows you to define whatever you'd like as the array index.

#include <unordered_map>
#include <utility>
#include <vector>

using std::tr1::unordered_map;
using std::pair;
using std::vector;

int main() {
    unordered_map<int, int> golfclap;
    golfclap[-10] = 9001;

    unordered_map<std::string, int> stdclap;
    stdclap["AIDS"] = 9001;

    unordered_map< vector< pair<string, string> >, pair<int, int> > gagglefuck;
    vector< pair<string, string> > bob;
    pair<string, string> faggot;
    pair<int, int> faggot2;
    faggot.first = "dohoho";
    faggot.second = "HISS PREPROCESSOR";
    faggot2.first = 9001;
    faggot2.second = 8001;
    bob.push_back(faggot);
    gagglefuck[bob] = faggot2;

    return 0;
}

Name: Anonymous 2009-01-27 1:26

>>8
Fuck, I forgot my #include <string>

using std::string;


Oh well. That's what makes it ENTERPRISE CODE.

Name: Anonymous 2009-01-27 1:57

>>8-9
Valid but a hash map is slower and takes a good deal more memory than a true array.

#define for negative indexed array
Go back to www.dailywtf.com, please.

Name: Anonymous 2009-01-27 6:16

Just do something like this:
int a[20];
int *p = &a[10];
p[-10] = 0;

Name: Anonymous 2009-01-27 6:20

>>4
I want an array that holds a number for each point in a 2D grid, but I want the origin (point (0,0)) to be in the middle, so I want to use negative indicies.
No, you don't.

Name: Anonymous 2009-01-27 6:29

#define -10 0
#define 9 1
#define 8 2
#define 7 3
#define 6 4
#define 5 5
#define 4 6
#define 3 7
#define 2 8
#define 1 9
#define 0 10

Name: Anonymous 2009-01-27 6:30

>>13
goddamnit, i fucked up

#define -10 0
#define -9 1
#define -8 2
#define -7 3
#define -6 4
#define -5 5
#define -4 6
#define -3 7
#define -2 8
#define -1 9
#define 0 10

thar's your answer OP

Name: Anonymous 2009-01-27 6:31

>>11
int *a = malloc(21 * sizeof int) + 10;

Name: Anonymous 2009-01-27 8:11

This is the most terrible! idea I've seen since that time I read a FrozenVoid post.

Name: Anonymous 2009-01-27 15:29

>>10
It is slower and does consume more memory, but I would prefer that to using unwieldy semantics like >>11,13-15

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List