WARP Project Forums - Wireless Open-Access Research Platform

You are not logged in.

#1 2007-Sep-18 13:17:10

jyhng
Member
Registered: 2007-Jan-25
Posts: 37

Problem with PowerPC code

Hi, I'm encountering a problem that I can best describe with this code:

Code:

#include "xbasic_types.h"
#include "xstatus.h"
#include "xio.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "xparameters.h"
#include "xuartlite_l.h"

#define NumberOfTestStructs 10

struct TestStruct
{
	int value;
};

static struct TestStruct TestStructArray[NumberOfTestStructs];

void SetPtr(struct TestStruct TestStructArray[], struct TestStruct* TestStructPtr);

int main()
{
	struct TestStruct* TestStructPtr;

	xil_printf("\n\r\n\rStarting Test Struct program");
	TestStructArray[0].value = 100;

	SetPtr(TestStructArray, TestStructPtr);

	// Try commenting/uncommenting this next line
	xil_printf("\n\rTest Struct Array [0]'s address: %d", &(TestStructArray[0]));

	// If the previous line were not called, then the address here would be different from the address in the function
	xil_printf("\n\rTest Struct Pointer's pointing to address: %d", TestStructPtr);

	// If the address in the line above is different from the address in the line above, then this next line will not return the correct value (100)
	xil_printf("\n\rTest Struct Pointer's value: %d", TestStructPtr->value);
	xil_printf("\n\r");
	while(1)
		XUartLite_RecvByte(STDIN_BASEADDRESS);
}


void SetPtr(struct TestStruct TestStructArray[], struct TestStruct* TestStructPtr)
{
	TestStructPtr = &(TestStructArray[0]);
	xil_printf("\n\rTest Struct Array [0]'s address: %d", &(TestStructArray[0]));
	xil_printf("\n\rTest Struct Pointer's pointing to address: %d", TestStructPtr);
	xil_printf("\n\rTest Struct Pointer's pointing to address's value: %d", TestStructPtr->value);
}

As you can see in the comments in between the lines of code, if I run the program as is, HyperTerminal would display the correct value of 100 for TestStructPtr->value.  But if I comment out that line that references TestStructArray[0]'s address, the value that is printed out is not correct and the value of the TestStructPtr is different from the one displayed by the function SetPtr.  Can someone explain why this is?  It seems that TestStructPtr will actually change its address value if I reference another variable.

Thanks,
Joshua

Offline

 

#2 2007-Sep-18 15:40:12

jyhng
Member
Registered: 2007-Jan-25
Posts: 37

Re: Problem with PowerPC code

Nevermind, it is resolved.  Turns out I need to use a pointer to a pointer for the function.

Offline

 

Board footer