Skip to content

Instantly share code, notes, and snippets.

@travisperson
Created September 26, 2012 21:39
Show Gist options
  • Save travisperson/3790762 to your computer and use it in GitHub Desktop.
Save travisperson/3790762 to your computer and use it in GitHub Desktop.
Storing both a double and a string in an array.
#include <string.h>
#include <stdio.h>
#define STRING 1
#define DOUBLE 2
typedef struct _my_data
{
int myDataType;
double myDouble;
char myString[20];
} MyData;
int main ()
{
MyData data[2];
int count = 0;
data[0].myDataType = STRING;
strcpy(data[0].myString, "This is a String");
data[1].myDataType = DOUBLE;
data[1].myDouble = 1.0;
for ( count = 0; count < 2; count++ )
{
if ( data[count].myDataType == STRING )
{
printf("Its a string! %s\n", data[count].myString);
}
else if ( data[count].myDataType == DOUBLE )
{
printf("Its a double! %lf\n", data[count].myDouble);
}
else
{
printf("I don't know what kind of data that is.\n");
}
}
return 0;
}
@whyrusleeping
Copy link

Interesting, whats this for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment