2 Replies - 206 Views - Last Post: 23 October 2011 - 11:15 AM Rate Topic: -----

#1 Akelo  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 98
  • Joined: 12-December 07

Arrays, Pointers, and bears, oh my! Clarification on my code.

Posted 23 October 2011 - 09:51 AM

Question 1: I thought that when you passed a variable to a function it copied it and put it into the stack in a different area of memory, unless you specifically passed it a pointer. Now, since I'm passing it an array, why does it not create a copy of the array? It appears that it refers to the same array (which I do not want).
Question 2: Second, linked to the first, how do I pass an array so that it copies itself?
Question 3: Lastly, the below code is giving me problems. The compiler whines that "line 36: error: lvalue required as increment operand". I know what the compiler is telling me, but I don't see why channels++ won't work...it works in the display_b() function.

Code in question
for(i=0;i<ARRAYSIZE;i++)
	{
		printf("Address: %p\tValue: %d\tMemory Space: %d bytes\n",&(*channels),*channels,sizeof(*channels));
		channels++;
	}



Full code (i have the channels++; line commented out so it compiles and runs so you can see what I'm trying to do)
#include <stdio.h>

#define ARRAYSIZE 7

//FUNCTION PROTOTYPES
void display_basicvariable(int x);
void display_a(int inputArray[]);
void display_b(int inputArray[]);

//MAIN FUNCTION
int main()
{
	int channels[ARRAYSIZE]={2,4,5,7,9,11,13};
	int i;
	int p = 0;
	printf("=======================");
	printf("\tRegular Variable outside of function (display_basicvariable()) scope\n");
	printf("Address: %p\tValue: %d\t Memory Space: %d bytes\n",&p,p,sizeof(p));
	printf("=======================");
	printf("\tRegular Variable inside of function (display_basicvariable()) scope\n");
	display_basicvariable(p);
	printf("=======================");
	printf("\tArray outside of function (display_a()) scope (using *channel+i)\n");
	for(i=0;i<ARRAYSIZE;i++)
	{
		printf("Address: %p\tValue: %d\tMemory Space: %d bytes\n",(void *)(channels+i),*(channels+i),sizeof(*channels+i));
	}
	printf("=======================");
	printf("\tArray inside of function (display_a()) scope (using *channels+i)\n");
	display_a(channels);
	printf("=======================");
	printf("\tArray outside of function (display_a()) scope (using *channels)\n");
	for(i=0;i<ARRAYSIZE;i++)
	{
		printf("Address: %p\tValue: %d\tMemory Space: %d bytes\n",&(*channels),*channels,sizeof(*channels));
		//channels++;
	}
	printf("=======================");
	printf("\tArray inside of function (display_b()) scope (using *channels)\n");
	display_b(channels);
	printf("=======================\n");

	return 0;
}




//FUNCTION DEFINITIONS
void display_basicvariable(int x)
{
	printf("Address: %p\tValue: %d\t Memory Space: %d bytes\n",&x,x,sizeof(x));
}

void display_a(int inputArray[])
{
	int i;
	for(i=0;i<ARRAYSIZE;i++)
	{
		printf("Address: %p\tValue: %d\tMemory Space: %d bytes\n",(void *)(inputArray+i),*(inputArray+i),sizeof(*inputArray+i));
	}
}

void display_b(int inputArray[])
{
	int i;
	for(i=0;i<ARRAYSIZE;i++)
	{
		printf("Address: %p\tValue: %d\tMemory Space: %d bytes\n",&(*inputArray),*inputArray,sizeof(*inputArray));
		inputArray++;
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Arrays, Pointers, and bears, oh my! Clarification on my code.

#2 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Arrays, Pointers, and bears, oh my! Clarification on my code.

Posted 23 October 2011 - 10:49 AM

When an array is passed as an argument it decays to a pointer to the first element of that array. The easiest way to avoid this is to wrap the array in an object:

struct Array
{
    int data[ARRAYSIZE];
};

...

Array myArray;
myArray.data[0] = 5;

Array myOtherArray = myArray;


This array-to-pointer decay is also why inputArray++ in display_b is valid, but channels++ is not valid in main. A pointer can be incremented, an array cannot.

Note that specifying int inputArray[] as a function parameter is equivalent to specifying int* inputArray.

This post has been edited by Coding in the name of: 23 October 2011 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Akelo  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 98
  • Joined: 12-December 07

Re: Arrays, Pointers, and bears, oh my! Clarification on my code.

Posted 23 October 2011 - 11:15 AM

had a feeling that was the case :). Mucho appreciado good sir.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1