digital adj. Having digits.     peer n. A comrade; a companion; a fellow; an associate. inmotion    
   
Recent Articles
Handling a Subversion Repository URL Change
Sunday, May 3, 2009
If your repository URL changes, you can use the following command to fix existing snapshots.
vfat Mounts Default to Lowercase Shortnames
Tuesday, April 21, 2009
I want a "this is brain-damage" quote from Linus for this mess.
VirtualBox or VMWare Virtual Machine at Login
Sunday, April 12, 2009
How to start a virtual machine in X when a user logs in.
Dialog Progress Bar Through Pipe
Sunday, April 12, 2009
How to use dialog to display a script progress bar and communicate progress to it through a named pipe.
Mount JFFS2 Image
Saturday, October 25, 2008
Example of how to mount a JFFS2 image using mtdblock.
Ottawa Linux Symposium 2008
Sunday, July 27, 2008
Here are some pictures from the 2008 Linux Symposium.
Linux Symposium 2008
Sunday, July 20, 2008
I'll be attending the Linux Symposium this year.
Clay Shirky: Institutions vs. collaboration
Monday, July 14, 2008
This is a rather interesting talk that takes some very foundational ideas from open source software development, P2P networks, and social networking and implies that these paradigms can apply to a lot more.

If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it.
- Richard Pattis

Projects-Code Snippets-Using Arrays...

Using Arrays with Macros

Friday, July 1, 2005 by digitalpeer

Just playing around with the idea of array bounds checking by using macros in C instead of using void pointers and real code. While usable, I still can't get around the fact that it won't work everywhere.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define DECLARE_ARRAY(type,name,size) \
	extern type array_##name[size];
	
#define DEFINE_ARRAY(type,name,size) \
	static const int size_##name = size; \
	type array_##name[size];

#define ARRAY_INIT(name,value) \
	do { \
	memset(array_##name,value,size_##name * sizeof(array_##name)); \
	} while (0);

#define ARRAY_GET(name,index,result) \
	do { \
	assert(index >= 0 && index < size_##name && result); \
	*result = array_##name[index]; \
	} while (0);

#define ARRAY_SET(name,index,value) \
	do { \
	assert(index >= 0 && index < size_##name); \
	array_##name[index] = value; \
	} while (0);

#define ARRAY_SIZE(name) size_##name
	
DECLARE_ARRAY(int,myarray,5)
DEFINE_ARRAY(int,myarray,5)
	
int main()
{
	int value;
	int x;
	ARRAY_INIT(myarray,0)
	ARRAY_SET(myarray,0,1)
	ARRAY_SET(myarray,1,1)
	ARRAY_SET(myarray,2,5)
	ARRAY_SET(myarray,3,1)
	ARRAY_SET(myarray,4,1)
	
	for (x = 0; x < ARRAY_SIZE(myarray);x++)
	{
		ARRAY_GET(myarray,x,&value)
		printf("value: %d\n",value);
	}
	
	
	return 0;	
}


Submit Comment to This Article - Be the first!
Please post a comment if you have something to add, find something wrong, or would like more information on the topic at hand. Do not use the comment form to contact the author about unrelated concerns!

Name: Email (optional):
Enter verification number here: