LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Pointers Pointers Pointers (https://www.linuxquestions.org/questions/programming-9/pointers-pointers-pointers-151476/)

urzumph 02-28-2004 04:32 AM

Pointers Pointers Pointers
 
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?

Any thoughts?
TIA.

cjcuk 02-28-2004 05:08 AM

Re: Pointers Pointers Pointers
 
Quote:

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.

itsme86 02-28-2004 10:41 AM

Creating a function pointer:

Code:

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");
}


krajzega 02-28-2004 11:31 AM

Quote:

Originally posted by itsme86


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");
}

[/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.
;-)

urzumph 02-28-2004 05:25 PM

Re: Re: Pointers Pointers Pointers
 
Quote:

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.
:cry: 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.

haobaba1 02-28-2004 09:59 PM

Re: Re: Re: Pointers Pointers Pointers
 
[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.

vasudevadas 02-29-2004 09:41 AM

Further to what krajzega said, you can reallocate to a new size a memory array you have already allocated, using realloc():

Code:

#include <stdlib.h>

void *realloc(void *ptr, size_t size);


kalleanka 02-29-2004 10:03 AM

I do not like them and I think with todays computers we dont need them for highlevel programing.

shortfuse 02-29-2004 04:41 PM

Actually C++ make this extremely easy with STL(Standard Template Library).

Code:

#include <vector>

using namespace std;

int main()
{

  vector<int> IntArr;

  /* load an array with integers */
  for( int i=0; i < 10; i++ ) {
    IntArr.push_back(i);
  }
  return 0;
}


bigearsbilly 03-11-2004 09:49 AM

Pointers to functions.

boy do they make code hard to read!
I used to do them, until i had to do some maintenance!

billy


All times are GMT -5. The time now is 11:08 PM.