Welcome, Guest. Please login or register.

Author Topic: Mr. Biehl learns C  (Read 6986 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Mr. Biehl learns C
« on: April 06, 2005, 06:11:48 AM »
Ok.  I've taken it upon myself to learn C as part of a self improvement program I've embarked upon and also to help clean up the environment.  As is normal with endeavors such as these I will have questions.  Please bear with me and my newbie status.

Pointers are my first hurdle.  I think I understand what they are and what they do but not really what to use them for.

To declare a pointer I do this:

Quote
int *i;


right?

so if I do:

Quote
int x = 5;
int *i;

i = &x;


then i will equal the memory address of x but not the value of x, right?

If I'm good so far, why would I really need the memory address of x?  What would be a real world example of the usage for this?  :-?

edit: I sure wish that we had a general developer section in the forums for this type of thing.
 

Offline Doppie1200

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 497
    • Show only replies by Doppie1200
Re: Mr. Biehl learns C
« Reply #1 on: April 06, 2005, 07:32:43 AM »
Correct pointer i points to x. Just like you said.

I think pointers are among the most usefull things in c.
You can allocate pieces of memory and use a pointer to access the memory. You can use pointers to manipulate arrays, structures all kinds of things. You can also use function pointers.

Play around with it. Look at various examples on the web: www.codeguru.com www.codeproject.com. Warning: those sites are very win32.

Good luck
Regards,
Erno

(O\\\\_|_/O) <- this is supposed to look like the front of my beetle
(entire front not possible in signature)
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Re: Mr. Biehl learns C
« Reply #2 on: April 06, 2005, 07:58:29 AM »
Quote
Warning: those sites are very win32.


You aint kidding. :-)

I've looked at several tutorials on the web and most use simple "hello world" type examples, which don't really tell me where's the appropriate place to use them.  I am assuming that one use for them would be to reduce the number of global variables by being able to reference a local variable in a function but somehow I believe there are other ways to do this.

For example:
Quote

 int main()
{
    int j;
    int k;
    int l;
    int *pt1;    /* Declares an integer pointer */
    int *pt2;    /* Declares an integer pointer */
    float values[100];
    float results[100];
    float *pt3;    /* Declares a float pointer */
    float *pt4;    /* Declares a float pointer */

    j = 1;
    k = 2;
    pt1 = &j;    /* pt1 contains the address of the variable j */
    pt2 = &k;    /* pt2 contains the address of variable k */
    pt3 = values;
        /* pt3 contains the address of the first element of values */
    pt3 = &values[0];
        /* This is the equivalent of the above statement */

    return 0;
}


I found this here and it does a great job of telling me how to use them but not when or where to use them.  Especially referencing a variable's memory address.

Man, I hate feeling clueless. :-?
 

Offline Chunder

  • Full Member
  • ***
  • Join Date: Dec 2002
  • Posts: 148
    • Show only replies by Chunder
Re: Mr. Biehl learns C
« Reply #3 on: April 06, 2005, 09:32:41 AM »
Try the following sites - they may have useful information, or at the very least, a wider audience to ask your questions of...

http://amigadev.amigaworld.net/
http://utilitybase.com/
Pimp My Amiga  8-)
___________________________
\\"The following statement is true.\\"
\\"The previous statement is a lie.\\"
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Mr. Biehl learns C
« Reply #4 on: April 06, 2005, 09:46:22 AM »
You were correct in your examples, so your understanding what the & and * operators are for is good.

To be honest, one does not use the &-operator often for the reason you already figured out for yourself: there is little information in the address itself. However, if you look at them a little differently, namely what these addresses point at, they become very useful. For nifty programs as well as shooting yourself repeatedly in the foot with a double-barrelled shotgun. I ain't kiddin' ya.

To give an example where pointers are invaluable, consider allocating memory. There are various ways of doing that on the Amiga: you can call on the system function AllocMem(), but if your fancy runs to programs which should run on other systems too, you would use malloc(). In any case, these functions do some internal housekeeping, ask the OS whether you can have what you ask for, and some other things we needn't go into right now. What interests us at the moment is what they return: a pointer! That pointer is (of course) an address, but it's pointing at an area of memory which is now yours to abuse. You access that memory via the pointer. Here's an example:
Code: [Select]
   void *MyMem;
    MyMem = AllocMem(1000, MEMF_PUBLIC);

MyMem now 'points' to an area in memory where we have 1000 bytes at our disposal. To access that memory, we need to apply a second technique, because we've defined MyMem to point at 'void'---in other words, an unknown type. (Make it a habit to try and avoid void where you can.) Suppose we want 1000 integers:
Code: [Select]
   int *MyMem, j;
    MyMem = (int *)AllocMem(1000*sizeof(int), MEMF_PUBLIC);
    for (i=0; j<1000; j++)
        MyMem[j]=j;

First we declare MyMem to be a pointer to int, and then we allocate the memory. The first (int *) is called a cast, an instruction to the compiler to change the type of the result of the proceeding expression to what you specify. AllocMem() returns a void *, and that is not equal to an int *. Note that 'equal' means 'equal in size as well as type': while there is no size difference between int * and void *, there is a type difference. (You could cast a char into an int by doing (int), but that would likely crash the machine as an int is 32 bits, while a char only 8. You'd be accessing a region of 8 bits as if it contained 32, and that is likely going to crash the program.) Then we need to take into account that an int takes not one byte, but four, but we don't specify the four: we specify its size transparently by writing sizeof(int).

And after all that, MyMem is now indistinguishable from an array. You can access it as if you had written:
Code: [Select]
   int MyMem[1000], j;
    for (i=0; j<1000; j++)
        MyMem[j]=j;

The assignment is in fact compiled to exactly the same code as in the pointer-example.

There are more uses to pointers: they provide a fast means of handing over large amounts of data without unnecessary copying. Suppose that you are working on an image processing program and need to pass on the information of a large graphic from one function to another. You can ask the system to copy that information over every time, but that is a huge waste of resources. It is much easier and faster to have one function tell the other: look, here's a pointer to where you can find the information you need, happy processing. You will see that happen a lot of times once you begin with Amiga system programming. In fact, the Amiga would not exist if it weren't for this property.

It is also the reason why pointers are such great ways of shooting yourself in the foot. There are no checks on their validity: you cannot tell whether a pointer is actually pointing at the information it is supposed to be pointing. That is why you should always strive to use casts: it helps the compiler help you because it is much better at spotting type inconsistencies. That is still no guarantee things are bug-free, but at least the most glaring errors will be caught. (The disadvantage is that if things do go wrong, debugging is not fun at all.)

I hope this has improved your understanding why you would want to use pointers. But don't assume that you can't do without them: many modern languages don't have pointers any longer and rely on other means of passing information along.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Re: Mr. Biehl learns C
« Reply #5 on: April 06, 2005, 05:54:12 PM »
@Cymric

This is exactly what I was looking for.  :pint:

I'm at work right now so I will have to wait until I get home before I can digest this completely.

Kudos.

@chunder

thanks for the links as I will give them a look. :-)
 

Offline mrsad

  • Newbie
  • *
  • Join Date: May 2003
  • Posts: 43
    • Show only replies by mrsad
    • http://members.chello.be/ws36055/
Re: Mr. Biehl learns C
« Reply #6 on: April 06, 2005, 06:40:17 PM »
pointers are also used for lists, predefined arrays can only be as long as well, the size you predefined for usage. but an array made out of pointers can go on for as long as you have memory available. see the following page for examples and more pointers advise: http://www.cs.cf.ac.uk/Dave/C/node10.html
Sticking feathers up your butt does not make you a chicken.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2281
  • Country: us
  • Gender: Male
    • Show only replies by SamuraiCrow
Re: Mr. Biehl learns C
« Reply #7 on: April 06, 2005, 07:25:24 PM »
If you ever used the "makelink" command from the CLI then you know how to use pointers.  If you don't know how to use it then it might help to look at any AmigaDOS documentation you might have on it.

For a more complete idea of how pointers work: "hard" links are like references in C++ and "soft" links are like standard pointers in C.

Some of the most useful uses of pointers are linked lists and trees.  Both of which you should get familiar with in a hurry because linked lists are used everywhere in AmigaOS and trees are used in directory structures.
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Re: Mr. Biehl learns C
« Reply #8 on: April 07, 2005, 01:09:12 AM »
@ mrsad:

Thanks for the link ... this is one of the more useful sites I've seen explaining this stuff.

@ samuraicrow:

Quote
If you ever used the "makelink" command from the CLI then you know how to use pointers. If you don't know how to use it then it might help to look at any AmigaDOS documentation you might have on it.


I've never really used the makefile command though I've come across it in documentation.  With the new light on the info I am into my AmigaDOS manuals as I type.

Quote
Some of the most useful uses of pointers are linked lists and trees. Both of which you should get familiar with in a hurry because linked lists are used everywhere in AmigaOS and trees are used in directory structures.


Thanks for the heads up.  I'm looking at a linked list tutorial right now.
 

Offline Generale

  • Full Member
  • ***
  • Join Date: Feb 2005
  • Posts: 233
    • Show only replies by Generale
    • http://members.dodo.com.au/~izabellion
Re: Mr. Biehl learns C
« Reply #9 on: April 07, 2005, 03:16:26 AM »
I did (most of) a bachelor of computer science, so I had to buy a _lot_ of prescribed textbooks. A fair few of them involved c/c++. I can tell you most books out there on the subject are useless, or a headache at best.

I came across teach yourself c++ in 24 hours on sale at the local angus & robertson. I usually hate the 'teach yourself' series of books, but this one is actually useful. If you see one floating around for a few dollars/euro/yen/drachma etc I'd say, grab it.

I'm going to go through it again, because it's been too long since I've done any programming. I don't even understand any of my old stuff.

You seem to be well on track though. Keep it up!
A500 (salvo): 1Mb RAM (512k chip, 512k SlowFast)
1x880k Floppy Disk, 1xIBM 540Mb 3.5\\"HDD KS1.3, WB1.3
1084S monitor. AT Keyboard!
A500 (Whitey): 512k RAM, 1x880k Floppy Disk, KS1.2, WB1.3
 

Offline MskoDestny

  • Sr. Member
  • ****
  • Join Date: Oct 2004
  • Posts: 363
    • Show only replies by MskoDestny
    • http://www.retrodev.com
Re: Mr. Biehl learns C
« Reply #10 on: April 07, 2005, 03:33:12 AM »
Another use is to allow a function to modify values from the calling function.  This can be useful when you need to return more than one value or you need to return a large structure or an array.

Also keep in mind that arrays are just pointers with some statically allocated memory set aside for them ahead of time.  Some examples:

char string1[] "Hello ";
char string2[] "World\n";
char * tmp;

tmp = string1;
string1 = string2;
string2 = tmp

printf("%s%s", string1, string2);

would print
World
Hello

string1[2] is equivalent to *(string1 + 2)
 

Offline Waccoon

  • Hero Member
  • *****
  • Join Date: Apr 2002
  • Posts: 1057
    • Show only replies by Waccoon
Re: Mr. Biehl learns C
« Reply #11 on: April 07, 2005, 03:36:28 AM »
Quote
I can tell you most books out there on the subject are useless, or a headache at best.

No kidding.  I've learned a hell of a lot more about programming once I left college than while I was in school.

Be careful with pointers, though.  A lot of times you don't need them.  Spend some time to learn all the types and methods supported by your compiler, first.  I did some quick and dirty stuff with Lattice a long time ago, but I'm not sure if Amiga compilers are as standardized as most PC compilers.
 

Offline DethKnight

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 509
    • Show only replies by DethKnight
Re: Mr. Biehl learns C
« Reply #12 on: April 07, 2005, 03:46:11 AM »
more power to ya here

I still prefer asm

who needs syntax rules when you can just "flip the switches"
metaphorically speaking

wanted; NONfunctional A3K keyboard wanted
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Re: Mr. Biehl learns C
« Reply #13 on: April 07, 2005, 03:56:35 AM »
@ Generale
Quote
I came across teach yourself c++ in 24 hours on sale at the local angus & robertson. I usually hate the 'teach yourself' series of books, but this one is actually useful. If you see one floating around for a few dollars/euro/yen/drachma etc I'd say, grab it.


I'll keep an eye (or two) out for it.  Seem to remember seeing it at Barnes and Nobles bookstore.

Quote
I'm going to go through it again, because it's been too long since I've done any programming. I don't even understand any of my old stuff.


 :lol:

@ MskoDestny
Quote
Another use is to allow a function to modify values from the calling function.


Is there another way to do this without pointers?  The answer is probably plain and clear in front of my face but right now I feel like I can't see the forest for the trees.  If you know what I mean.

@ Wacoon
Quote
Be careful with pointers, though. A lot of times you don't need them. Spend some time to learn all the types and methods supported by your compiler, first. I did some quick and dirty stuff with Lattice a long time ago, but I'm not sure if Amiga compilers are as standardized as most PC compilers.


Maybe I should learn on my Linux system instead.  I've read that pointers are super useful but easy to mess up, that is the main reason why I'm trying to wrap my head around them now.

@ Everyone

Thanks for all the responses.  You all have been super helpful.  :pint:
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show only replies by ottomobiehl
Re: Mr. Biehl learns C
« Reply #14 on: April 07, 2005, 03:59:07 AM »
Quote

DethKnight wrote:
more power to ya here

I still prefer asm

who needs syntax rules when you can just "flip the switches"
metaphorically speaking



Ah, to the metal.  I dabbled in asm way back in the 80's when I had my C64 (but was never that good) and wouldn't mind going back to learn.  Seems like it has gotten way more complicated now.  Maybe this will be something I pick up when I get more comfortable with C.