Welcome, Guest. Please login or register.

Author Topic: Mr. Biehl learns C  (Read 7138 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
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.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Mr. Biehl learns C
« Reply #15 on: April 07, 2005, 08:27:59 AM »
Quote
MskoDestny wrote:
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:

One word of caution: do not try and modify such strings (i.e., make them lower case, turn it into l33t) without making a copy of them first if you want your programs to be portable. Many OSes allocate such strings in read-only memory and dump core with a segmentation fault if you try and modify them. Amigas do not know that concept, so will happily allow you to modify them.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

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 #16 on: April 07, 2005, 12:06:24 PM »
Pointers can also have other pitfalls, especially when your process is threaded or uses system global values. Essentially in any concurrent system dealing with I/O, external APIs or IPC.
If you are using pointers, there is no guarantee that the values of the data in the location will be the same as they were. Other threads/processes can change the values on you.

If you need the value to be the same as when you entered the function, you either need to stop your process from being pre-empted (varies between OSes), call by reference, or utilise some sort of semaphor or mutex.

I can't believe I know this stuff  :-o

Anyhow, if you're going to use linux/unix there are a lot of very nice, but non-ansi things to be aware of. By all means utilise them, but don't rely on them.

Assembler is still easy....just not MIPS RISC. Well, it's easy.....but time consuming. A lot like moving a mountain with a pair of tweezers, a grain at a time.

Anyway, yeah look out for the teach yourself c++ book. it's good value. When pointers and casting make my eyes glaze over, I look at that book.
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 Sidewinder

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show only replies by Sidewinder
    • http://www.liquido2.com
Re: Mr. Biehl learns C
« Reply #17 on: April 07, 2005, 12:50:03 PM »
Quote
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.


The only other way to modifiy a value inside a function is to make the value global (declared outside all functions).  Using global variables we can make the code very simple in small programs, but in larger programs it actaully can make things more confusing.

Code: [Select]

int x;  /* declare x as global, outside of all functions */
...
x = 10;
printf("%d\n", x);

do_something();   /* call function with address of x */

printf("%d\n", x);
...
void do_something(void)
{
   x = x + 10;
}


Globals aren't bad, but use them in moderation and only when needed.  Be careful to make the global varaible name something unique so it won't cause unexpected problems in other functions.

If x is not global the code looks like this:

Code: [Select]

...
int x;
x = 10;
printf("%d\n", x);

do_something(x);  /* call function with a copy of x */

printf("%d\n", x);
...
void do_something(int x)
{
   x = x + 10;
}


This will not modify x in the calling function.  The reason is that x is passed by value to the do_something() function.  This means that a new copy of x is made and modified inside the function.  Once the function is complete, this new copy is destroyed and the original copy is used again.

We can use pointers to modify the original x value like this:

Code: [Select]

...
int x;
x = 10;
printf("%d\n", x);

do_something(&x);   /* call function with address of x */

printf("%d\n", x);
...
void do_something(int *x)
{
   *x = *x + 10;
}


This time the a new variable is still created, but instead of holding a copy of the value of x, it holds a copy if the address of x.  This is called "pass by reference".  The do_something() function was changed to modify the value pointed to by x.  This time, the value of x in the calling function should've been changed.
Sidewinder
 

  • Guest
Re: Mr. Biehl learns C
« Reply #18 on: April 07, 2005, 12:55:01 PM »
@Otto

I teach C/C++ as a student teacher, if you like I can send you PDF files of the handouts I create for my students.

PM me with your email address if you are interested.
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show only replies by PiR
Re: Mr. Biehl learns C
« Reply #19 on: April 18, 2005, 07:29:34 PM »
Quote

MskoDestny wrote:

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

tmp = string1;
string1 = string2;
string2 = tmp



Yiekes!

To be honest this won't even compile.
Array names CAN BE CONVERTED to pointers, but are not equally pointers. So impressions, like string1 = string2 or string2 = tmp won't go, sorry.

Happy  compiling
PiR
 

Offline woof

  • Jr. Member
  • **
  • Join Date: Feb 2003
  • Posts: 94
    • Show only replies by woof
    • http://uae.is.free.fr
Re: Mr. Biehl learns C
« Reply #20 on: April 18, 2005, 08:33:48 PM »
Just consider pointers as arrays ;-)
As  with a pointer like
char *tab;
char c;
you can do *tab=c; or the SAME tab[0]=c; so a pointer is an (unknown size) "array"

If your pointer ("array") come from a malloc() it give
you access to a block ("array") of memory

If it is the screen data pointer = it is an array of pixel
tab[320*3+64]=0; can set up as black the (x 64 y 3) pixel of a 256  colors screen ("array" of pixels)

If it is a string pointer so can change chars
tab[0]='H'; tab[1]='E'; tab[2]='L'; tab[3]='L'; tab[4]='O'; tab[5]=0; ("array" of chars)


Alain
 :-D
 

Offline minator

  • Hero Member
  • *****
  • Join Date: Jan 2003
  • Posts: 592
    • Show only replies by minator
    • http://www.blachford.info
Re: Mr. Biehl learns C
« Reply #21 on: April 18, 2005, 11:24:46 PM »
Check out:
"C/C++ Programmer's Reference" by Herbert Schildt

link.

It's no a gigantic wordy book which goes into intricate detail, it's short and to the point.

Unlike most programming books, it's cheap:-)
 

  • Guest
Re: Mr. Biehl learns C
« Reply #22 on: April 18, 2005, 11:50:07 PM »
I really recommend Thinking In C++ Volume 1 and 2 by Bruce Eckel, they are just about the best C++ books available IMHO.

Download both books here
 

Offline Mordo

  • Newbie
  • *
  • Join Date: Jan 2005
  • Posts: 5
    • Show only replies by Mordo
Re: Mr. Biehl learns C
« Reply #23 on: April 19, 2005, 12:52:36 AM »
Also, I would recommend the following site especially if you want to get a real understanding of pointers:

http://cslibrary.stanford.edu/

Good tutorial on C, linked lists, trees and more...

Mordo
 

Offline ottomobiehlTopic starter

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

  • Guest
Re: Mr. Biehl learns C
« Reply #25 on: April 19, 2005, 02:33:37 AM »
Quote

ottomobiehl wrote:
@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


It all depends on what you want to develop really.

Low level stuff, stick with C.  User level stuff, either will do but I recommend C++ as to a certain extent it's backwardly compatible due it just being an extension of C.

BeOS is the only OS that I know of where 99% of the entire OS (including the lowest level stuff)was written in C++.  It has a lovely API too if you are a C++ developer.
 

Offline ottomobiehlTopic starter

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

Offline Waccoon

  • Hero Member
  • *****
  • Join Date: Apr 2002
  • Posts: 1057
    • Show only replies by Waccoon
Re: Mr. Biehl learns C
« Reply #27 on: April 19, 2005, 03:46:42 AM »
Quote
Ok, I'll start looking at C++ and the whole OOP theory to boot.

Just don't go crazy with OOP.  It was developed to solve a certain number of problems and it helps to know when not to use it.  :-)
 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show only replies by FluffyMcDeath
Re: Mr. Biehl learns C
« Reply #28 on: April 19, 2005, 06:58:41 AM »
Quote

ottomobiehl wrote:

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?



Correct, but unexciting. To get the value of x from the pointer you dereference the pointer thus:
e.g.
int a;
a = *i;

The '*' dereferences the pointer to give the value of what is pointed at. The value of what is pointed at can also be changed thus:

*i = 5;

Which puts 5 at the address pointed to by i.

However, imagine you have a nice big number of things you wished to pass around, like maybe a person and you wished to pass that person (or persons like it) to a collection of functions. Let's say a person has a number of attributes like:
height, weight, dayOfBirth, monthOfBirth, yearOfBirth, name, sex

Each time you wanted to pass all that stuff to a function the call would be tedious indeed.

Instead you can create a structure and just pass a pointer to the structure a la:

typedef struct {
   int height;
   int weight;
   int dayOfBirth;
   int monthOfBirth;
   int yearOfBirth;
   char * name;
   char sex;
} personT;

Note that name is a pointer to the start of a string of bytes.

The personT is now a type (like int or char) that you can use to declare variables as. Then you can set the values.

personT mrX;

mrX.height = 167;
mrX.weight = 70;
mrX.dayOfBirth = 5;
mrX.monthOfBirth = 6;
mrX.yearOfBirth = 1978;
mrX.name = "Fred X";
mrX.sex = 'M';

/* and now you can call a function that does something with persons */

printBirthday(&mrX);


and printBirthday would look something like...

void printBirthday(personT * person)
{
    printf("%s was born %d/%d/%d\n",
           person->name,
           person->dayOfBirth,
           person->monthOfBirth,
           person->yearOfBirth );
}

Note that when you are accessing members of a struct, use . (dot). When you are accessing members of a pointer to a struct use -> (arrow or points-to).




 

Offline FluffyMcDeath

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 3440
    • Show only replies by FluffyMcDeath
Re: Mr. Biehl learns C
« Reply #29 from previous page: April 19, 2005, 07:19:34 AM »
But there's more....

One of the most fun things you can do with pointers is screw yourself over in mysterious and dangerous ways.

e.g.

int *bob;

*bob = 12;

You have now written the value 12 into a totally unknown location in memory. If bob is global it'll probably be 0 but it might not be. If bob is on the stack (local to a function) it could point any old place. If you have memory protection and you have written to some memory that doesn't belong to you, or is inappropriate for writing data then you will get a grim reaper. If you don't have mem protection or you get unlucky and write to memory you own, you get a mysterious and bizarre failure that doesn't seem like it is anything to do with this code and maybe only sometimes! FUN!!!

Always make sure your pointer points at something sensible before you use it. Aim THEN fire.

Also, watch out if you are pointing at stuff on the stack.

This is OK:

int func1()
{
   int val = 4;
   return func2(&val);
}

int func2 (int* val)
{
    return *val * 2;
}

val is on the stack. You get a pointer to the stack address of val. You call the next func and the stack grows, val stays where it is, you still have the address, all is fine. The answer is 8.

If you try this:

int * func1()
{
    int num = 4;
    return #
}

int func2()
{
    int val*;
    val = func1();
    return func3(val);
}

int func3(val)
{
    int brianTheStrangeNumber = 23;
    return *val * 2;
}

now the answer could be 46, or it could be twice the value of the address of num, or something else depending on how the stack frames are laid out. That's some cool stuff.