LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2004, 04:32 AM   #1
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Rep: Reputation: 30
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.
 
Old 02-28-2004, 05:08 AM   #2
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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.
 
Old 02-28-2004, 10:41 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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");
}
 
Old 02-28-2004, 11:31 AM   #4
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
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.
;-)
 
Old 02-28-2004, 05:25 PM   #5
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Original Poster
Rep: Reputation: 30
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.
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.
 
Old 02-28-2004, 09:59 PM   #6
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
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.
 
Old 02-29-2004, 09:41 AM   #7
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
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);
 
Old 02-29-2004, 10:03 AM   #8
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
I do not like them and I think with todays computers we dont need them for highlevel programing.
 
Old 02-29-2004, 04:41 PM   #9
shortfuse
Member
 
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
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;
}
 
Old 03-11-2004, 09:49 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Pointers to functions.

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

billy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About pointers RHLinuxGUY Programming 4 11-15-2005 04:47 PM
pointers in c++ marios_auth Programming 1 06-16-2004 08:20 AM
Looking for some pointers... p3ngu!n Linux - Newbie 17 10-30-2003 06:46 AM
pointers in C again h/w Programming 9 10-29-2003 11:46 PM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration