nearly there
I have one issue at the moment. When i ask for memory location within the test data function it works fine. But I want it passed back to the main function so it can be displayed within the menu. How would you propose I do this?
Code:
/***********************************************************************//*Digital embedded retake*/
/*By Sam James JW004171*/
/***********************************************************************/
/***********************************************************************/
/*Inclusions*/
/***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
/***********************************************************************/
/*Indentifiers*/
/***********************************************************************/
int testData(void);
/***********************************************************************/
/*Main program*/
/***********************************************************************/
int main (void)
{
/***********************************************************************/
/*Declarations*/
/***********************************************************************/
unsigned char *hex;
int i;
/*Create test data in memory*/
testData();
/*Display menu*/
printf("Please make a choice: \n");
/*Choices*/
printf("1. Display contents of memory\n");
/*Choice made*/
scanf("%d", &i);
switch(i) {
case 1:
/*This doesn't work as intended*/
printf("Start of memory block: %p\n", hex);
printf("End of memory block: %p\n", hex + 65553 - 1);
default:
printf("Incorrect option");
}
return 0;
}
/*printf("Start of memory block: %p\n", hex);
printf("End of memory block: %p\n", hex + 65553 - 1);*/
/***********************************************************************/
/*Setup test data*/
/***********************************************************************/
int testData(void)
{
/*Declarations*/
unsigned char *hex;
int t = 0;
int i = 0;
int p = 0;
int x = 0;
char str[3];
char testData1[65] = "BDB653CC0800FD1018FD101AFD101CFD101EBDB665CC0002DD007F00067F0010";
/*Allocate memory*/
hex = (unsigned char *)malloc(65553);
if (hex == NULL)
{
printf("Cannot allocate memory \n\r");
exit(1);
}
/*Store data*/
x = sizeof(testData1) / sizeof(char);
p = x-2;
for (i = 0; i <= p; i += 2)
{
strncpy(str, testData1 + i, 2);
str[2] = '\0';
hex[t++] = (unsigned char)strtoul(str, NULL, 16);
}
/* This works as intended*/
printf("Start of memory block: %p\n", hex);
printf("End of memory block: %p\n", hex + 65553 - 1);
}