Tuesday, August 12, 2008

set a memory location

Given the following function signature, write C code to set the memory location passed in (say, for an embedded system).

#include <stdlib.h>
#include <stdio.h>

void setMemoryLocation(int memoryLocation, int value)
{
    *((int*)memoryLocation) = value;
}

int main()
{
    int *pointer = malloc(sizeof(int));
    setMemoryLocation((int)pointer, 12345);
    printf("%d\n", *pointer);
    return 0;
}

No comments: