View unanswered posts | View active topics It is currently Fri Nov 21, 2014 12:38 pm






Reply to topic  [ 4 posts ] 
Pointers and Arrays in RobotC 
Author Message
Rookie

Joined: Thu Nov 07, 2013 7:40 pm
Posts: 4
Post Pointers and Arrays in RobotC
Basically I'm trying to use pointers as used in C. It mostly seems to be the same, aside from the obvious lack of malloc and calloc (though I have heard of a function called memset). So the main question is regarding arrays, and pointers to arrays.
For this function:
Code:
into access_element ( int* arr, int pos )
{
    return *(arr + pos);
}

And I call it like such.
Code:
const int ARR_SIZE = 6;
int foo[ARR_SIZE];
access_element(foo,2);  // Get the (uninitialized) value at pos 3

And along with that function I've tried other set-ups, such as int& arr as the parameter.
Is it possible in RobotC to pass the array position in a function?

P.S. How do I include other RobotC files? It seems that there are no header files (so #include "mylib.c") and whenever I try to compile an such a file it gives me am error that task main () is not defined, however when I place a task main inside of it it errors, saying that I cannot redefine task main.


Thu Nov 07, 2013 8:06 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Pointers and Arrays in RobotC
StevenChartis wrote:
Basically I'm trying to use pointers as used in C. It mostly seems to be the same, aside from the obvious lack of malloc and calloc (though I have heard of a function called memset).

Yes, there is a memset function and yes there are no dynamic memory allocation functions.
StevenChartis wrote:
Is it possible in RobotC to pass the array position in a function?

What do you mean by passing the array position in a function? Do you mean this?
Code:
void SetValue(int *element, int data)
{
    *element = data;
}

task main()
{
    int dataArray[6];

    SetValue(&dataArray[2], 40);
}

StevenChartis wrote:
P.S. How do I include other RobotC files? It seems that there are no header files (so #include "mylib.c") and whenever I try to compile an such a file it gives me am error that task main () is not defined, however when I place a task main inside of it it errors, saying that I cannot redefine task main.

RobotC has a very unusual concept of header files. You can include "header files" but their header files do not contain just definitions and prototypes, it may contain the actual code. In fact, RobotC only recognizes one C file which contains the "task main". If you have more than one C file, IDE will complain unless the C file that contains "task main" is the current file in the IDE. In summary, RobotC can only handles a monolithic C file (i.e. the entire program written in one C file). You can put sections of the code in different "header files" and #include them. But you can't put functions in separate C files and their definitions in a .h files and hope RobotC knows where to find these files. In other words, RobotC doesn't have an incremental linker (i.e. no makefiles or project files that describe multiple C files and their dependencies). Hope that answers your questions.


Last edited by MHTS on Sun Nov 10, 2013 1:59 pm, edited 1 time in total.



Sun Nov 10, 2013 4:48 am
Profile
Rookie

Joined: Thu Nov 07, 2013 7:40 pm
Posts: 4
Post Re: Pointers and Arrays in RobotC
Thank you so much for your answers. I think I made a typo in my second question, so you answered my question correctly, but I communicated the wrong question.
What I was really trying to find out, was whether I could pass an array's address to a function as such:
Code:
 // Note the int* arr.
void setValue ( int* arr, int pos, int val )
{
     // Note that this line would not work in RobotC.
    *(arr + pos) = val;
}

int myarr[128];
 // In RobotC the 'myarr' conflicts with
 // the 'int* arr' mentioned earlier.
setValue(myarr, 0, 52);    // same as myarr[0] = 52;

However, with your response I see a possibility in replacing
Code:
setValue(myarr, 0, 52);

with
Code:
setValue(&myarr[0], 0, 52);

I am not sure how well this would work (as I do not have RobotC at home, and I would still be left with the other conflict (the one inside the function), which may or may not be resolved with this fix.


Sun Nov 10, 2013 9:23 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Pointers and Arrays in RobotC
StevenChartis wrote:
However, with your response I see a possibility in replacing
Code:
setValue(myarr, 0, 52);

with
Code:
setValue(&myarr[0], 0, 52);

I am not sure how well this would work (as I do not have RobotC at home, and I would still be left with the other conflict (the one inside the function), which may or may not be resolved with this fix.

If you can pass a pointer of any element of the array to the function, why bother passing the pos parameter? If you can, avoid doing pointer arithmetic. It could be a source of very subtle bugs. For example, what if you pass a value of pos that is higher than the number of elements in the array? That will cause the code to overwrite random memory depending on where the pointer points to. In the best case, the code crashed right away so it may be easy to pinpoint the bug. In the worst case, some random location got overwritten and the code ran quite a while before something failed because of the altered data. But since the context is long gone, it is difficult to associate the failure with the fact that the memory was overwritten with garbage sometime ago.
If you call the function with:
Code:
setValue(&myarr[VeryHighIndex], 52);

The compiler should be able to catch the problem with "index out of bound" if "VeryHighIndex" is a constant. It still doesn't help you if "VeryHighIndex" is a variable and could be assigned with an out of bound index in run-time. In other words, be careful when you are playing with pointers.


Sun Nov 10, 2013 2:16 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.