ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Essentially I have several years non-formal self taught programming experiance in a tiny little language called euphoira. While I like the language (mostly) it's size & it's developers (I can't say I like his programming philosophies (sp)) mean, AFAICT, the language is going no-where.
Therefore I want to graduate to C / C++.
One thing to note here is that euphoria doesn't have pointers (as such...) and I am trying to understand what they can do, and what they can't do.
Firstly, I have seen referances to pointers to functions. This looks useful (replacement for routine_id in euphoria) Could anyone explain how it is done?
Secondly, One of the first things I want to re-impliment is Dynamic Length'd Arrays. (AKA sequences) (more for the experiance, I know there are libraries that do this already) to do this, I need to declare something as a pointer to something that I don't know what it is. so obviously *int doesn't work here. I was wondering if this may be achieved with *void. Perhaps I am missing something?
Originally posted by urzumph One thing to note here is that euphoria doesn't have pointers (as such...) and I am trying to understand what they can do, and what they can't do. [1]
Firstly, I have seen referances to pointers to functions. This looks useful (replacement for routine_id in euphoria) Could anyone explain how it is done? [2]
Secondly, One of the first things I want to re-impliment is Dynamic Length'd Arrays. (AKA sequences) (more for the experiance, I know there are libraries that do this already) to do this, I need to declare something as a pointer to something that I don't know what it is. so obviously *int doesn't work here. I was wondering if this may be achieved with *void. Perhaps I am missing something? [3]
[1] There a two main things that you can do with a pointer. This is referring to something else ( `pointing' ) and pointer arithmetic. The former allows you to track more complex data types ( such as the use of character pointers and null termination to make a string ), the latter allows you to seamlessly traverse an array of any type with language working out the memory increment to move from one element to the next.
[2] Function pointers are used internally anyway in pretty much anything. That is, there exists a reference as to where the function is so that it can be called. In C this mechanism is exposed to the programmer. The most common usage is so-called `callback' ( because the function can `callback' the argument ). You basically do a standard function declaration, but replace it's name with (*function_pointer) ( where function_pointer is the name you wish to give it. Note that the brackets are necessary to enforce precedence, as it would be interpreted as returning an extra pointer otherwise ). Simple common example, I write a sorting function, but I do not know how to compare the two data types I am given. I give the sort function an argument `int (*compar)(const void *, const void *)', to which you pass a function capable of making the comparison ( this example is actually from the qsort(3) function in the C standard library ). I am not sure if I have missed something, if you are unsure just ask.
[3] In general, a dynamic array is not that difficult to create within C. I would tend to use implementations that are intelligent as to the data type. If you want to implement a `dumb' representation then I would use void pointers and pass an argument for the size of each member ( eg, sizeof(struct my_struct) ). You do realize, however, that you will need to work out if the array needs extending yourself ? As it will not signal this to you. I suppose it is possible to cause it to, and it is easy in C++, but beyond the scope of an initial exercise I feel.
void myfunc(void)
{
// contents of function
}
int main(void)
{
void (*ptr_myfunc)(void) = myfunc;
myfunc();
ptr_myfunc(); // calls the function using the pointer. Equivalent to
// the call on the line above.
}
Creating a dynamic array:
Code:
int main(void)
{
int size;
int *array;
char buf[10];
printf("How many elements would you like in the array? ");
fgets(buf, sizeof(buf), stdin);
size = atoi(buf);
array = malloc(sizeof(int) * size);
if(array == NULL)
printf("Couldn't allocate memory for the array!\n");
}
int main(void)
{
int size;
int *array;
char buf[10];
printf("How many elements would you like in the array? ");
fgets(buf, sizeof(buf), stdin);
size = atoi(buf);
array = malloc(sizeof(int) * size);
if(array == NULL)
printf("Couldn't allocate memory for the array!\n");
}
[/B]
That's right, but it's one (maybe not important detail) : malloc is a ANSI C way, in C++ (as he asked) you use new and delete. It can be quite easier, because it doesn't matter which type are you declaring...new will allocate memory as big as you want. For example: new char array[50];
would allocate 50 bytes, but: new int array[50]
would allocate 200 bytes.
;-)
Originally posted by cjcuk
I am not sure if I have missed something, if you are unsure just ask.[1]
In general, a dynamic array is not that difficult to create within C. I would tend to use implementations that are intelligent as to the data type. If you want to implement a `dumb' representation then I would use void pointers and pass an argument for the size of each member ( eg, sizeof(struct my_struct) ). You do realize, however, that you will need to work out if the array needs extending yourself ? As it will not signal this to you. I suppose it is possible to cause it to, and it is easy in C++, but beyond the scope of an initial exercise I feel.
LQ.org ate my gigantic reply
OK, here's a shortened version :
[1] : how do I call the function from the pointer without knowing it's name? (as I would need to in the example posted by itsme)
[2]
/me thinks from the other replies that I am mis-using some terminology.
A sequence can be any length you want it to be, and it can change length at any time. (if you have used python, think lists) While it is simple enough to create an array that is any given length throughout the program, in a lot of the programs I don't know what length the array will be untill I am finnished out-putting to it. I know a way to do this with structs, but it's in-efficient. My current thought is with an expanding-as-nessisary number of statically sized arrays.
[2]
/me thinks from the other replies that I am mis-using some terminology.
A sequence can be any length you want it to be, and it can change length at any time. (if you have used python, think lists) While it is simple enough to create an array that is any given length throughout the program, in a lot of the programs I don't know what length the array will be untill I am finnished out-putting to it. I know a way to do this with structs, but it's in-efficient. My current thought is with an expanding-as-nessisary number of statically sized arrays. [/B][/QUOTE]
Personally I don't think you really want a dynamic array. Especially not if your dealing with any large set of data that may cause the array to get large. Each time you resize the array you would need to completely copy it therefore you would want to come up with some policy as to how much you are going to resize the array by. Doubling each time is common but could lead to your using much more memory than you need. The size of data that you normally deal with would be the determining factor in your resize policy.
What you probably want is a linked list or a tree structure that doesn't require you to request huge chunks of memory in advance.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.