I have so far been attempting to create a function that will allow me to read values from 4 ultrasound sensors attached to a HiTechnic Sensor Multiplexer, place these values into a 4-item array, and then read this array. So far, I have run into the array in functions problem and I have attempted a few work arounds to this issue that havent quite worked out.

Here is the code:

Code:

#pragma config stuffs.... this takes up a lot of room so i deleted it but its not the issue

//2nd Attempt: this gives me valuese for all 4 sensors but the values are all for msensor_s3_1 except that the 3rd value is the dist +1 and the 4th value is the dist +2.... so if the distance is 7 you get {7,7,8,9}

Your first attempt won't work even in ANSI C because you declared the array inside a function and attempted to return a pointer of the local array in the function. As soon as the code exited the function, the local array is out of scope and would be destroyed. So it won't work.For your 2nd attempt, RobotC does not support pointers. So you cannot use the "int *" syntax as the parameter to a function.I am surprise both of your attempts even compiled without error! In any case, if you want to pass an array to a function you need to define it in a structure and pass the structure by reference to the function. Something like this:

Thanks Xander, I was struggling with passing arrays to functions, not now!

Mon Dec 17, 2012 7:30 am

amcerbu

Novice

Joined: Sun Oct 21, 2012 10:01 pmPosts: 76

Re: Attempting a work-around for Array Functions Issue

MikeJMcFarlane wrote:

Thanks Xander, I was struggling with passing arrays to functions, not now!

You can also pass an array of ints to a function as a pointer to an int. The name of the array is actually a pointer to the first item of the array, so using the [ ] operators on that pointer is legal.

If I'm not mistaken, the a[b] operation is equivalent to *(a + b). In other words, item b of array a is the same as dereferencing the pointer at a + b. Pointer arithmetic dictates that adding a number b to a pointer returns a new pointer that's b items "later."

Mon Dec 17, 2012 3:28 pm

MikeJMcFarlane

Rookie

Joined: Thu Sep 13, 2012 9:43 amPosts: 47

Re: Attempting a work-around for Array Functions Issue

amcerbu wrote:

MikeJMcFarlane wrote:

Thanks Xander, I was struggling with passing arrays to functions, not now!

You can also pass an array of ints to a function as a pointer to an int. The name of the array is actually a pointer to the first item of the array, so using the [ ] operators on that pointer is legal.

If I'm not mistaken, the a[b] operation is equivalent to *(a + b). In other words, item b of array a is the same as dereferencing the pointer at a + b. Pointer arithmetic dictates that adding a number b to a pointer returns a new pointer that's b items "later."

Thanks amcerbu. Is it meant to be 'int * arrayParm' or 'int *arrayParm'? ie a space between the asterisk and the array name.

I think part of the problem was I had declared the array within main() so it was a local array (if that is the right term).

Tue Dec 18, 2012 4:53 am

amcerbu

Novice

Joined: Sun Oct 21, 2012 10:01 pmPosts: 76

Re: Attempting a work-around for Array Functions Issue

In RobotC (and all C-based languages, as far as I know), whitespace doesn't matter. That's why a+b and a + b are equivalent. The space is purely cosmetic, although it does make the code more readable. You may see it either way.

You can still pass a local array to a function, that probably wasn't the problem.

In RobotC (and all C-based languages, as far as I know), whitespace doesn't matter. That's why a+b and a + b are equivalent. The space is purely cosmetic, although it does make the code more readable. You may see it either way.

You can still pass a local array to a function, that probably wasn't the problem.

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum