Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
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 ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #1 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 ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #2 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 ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #3 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 ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #4 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 all replies
Re: Mr. Biehl learns C
« Reply #5 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.
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #6 on: April 19, 2005, 02:27:46 AM »
@all

Thanks for the examples and links.  They certainly have been helpful and I doubt I would have been able to find them on my own.  Just a quick question though, I've noticed that alot of you have mentioned C++ in your replies.  I had made the decision to learn C first before diving into C++ but have got to thinking that maybe I should be trying to learn that instead or in tandem.  Is this wise or should I stick it out with C and move to C++ a little later?

Thanks,

Dan
 

Offline ottomobiehlTopic starter

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 363
    • Show all replies
Re: Mr. Biehl learns C
« Reply #7 on: April 19, 2005, 02:44:33 AM »
Hi Nik,

I got your email and just sent you an email back stating so.  :-)

Ok, I'll start looking at C++ and the whole OOP theory to boot.  Seems like since I've started C seems pretty straight forward (so far) and I often wonder why I would be so intimidated to learn this stuff.  Just goes to show that sometimes reality isn't as bad as your mind makes it to be.

As for BeOS; I've never had a chance to try it out.  Suppose I should as I like to play with different OS's all the time.