LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 02-21-2004, 11:23 PM   #1
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
give me something to do or learn


hi

first off, im a newbie programmer

i know how how to do basic flow control statements like:
for, while , if , then

and i dont know where to go from here lol

right now im trying to learn pointers

and its really confusing - no i mean REALLY - i dont understand the point of it ! haha

well anyways if anybody can give me some kind of 'assignment' or something to learn that would be awesome

thanks guys
 
Old 02-21-2004, 11:35 PM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
make a linked list that you can traverse through forwards and backwards to find names and their corresponding phone numbers.

due in 3 days.
 
Old 02-21-2004, 11:38 PM   #3
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
hint: you need 5 pointers
 
Old 02-22-2004, 12:32 AM   #4
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
Re: give me something to do or learn

Quote:
Originally posted by Longinus
hi

first off, im a newbie programmer

i know how how to do basic flow control statements like:
for, while , if , then

and i dont know where to go from here lol

right now im trying to learn pointers

and its really confusing - no i mean REALLY - i dont understand the point of it ! haha

well anyways if anybody can give me some kind of 'assignment' or something to learn that would be awesome

thanks guys
The analogy that finally did it for me was one that involved Mailboxes. Just think of pointers like that for now, then later you can do pointer arithmatic and all kinds of scary stuff. Your house would be the class that you build and the pointer to your house would be the mailbox outfront. A pointer is just the address on the front of the mail that the mailman uses to find your house.

*var or var-> opens the door.


ok now for your assignment I will give you a week seeing as you have other hw.

Make a recursive binary search function that finds the location of an integer in a SORTED array in log(n) time, where n is the size of the array.

the function will take these arguments

int* array
int start
int finish
int value

its prototype will look like:

int search(int *array, int a, int b, int val);

Think divide and concure, how do you find stuff in the phone book? Look in the middle first!


Next assignment:

Sorting

Last edited by haobaba1; 02-22-2004 at 12:33 AM.
 
Old 02-22-2004, 08:06 AM   #5
jtshaw
Moderator
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 63
Ok, here is the John Shaw quick guide to pointers.

A pointer is a type that holds a memory address which contains data of the pointers type.

So if ptr is an int pointer and ptr = 0xF003 then there is an int at memory address 0xF003.

The operations are as follows:

*ptrvar = the value at the memory address the pointer is set to.
&nonptrvar = the memory address of a non pointer.

So if I have a regular int x and a an int pointer iptr and I want iptr to point to x I would say iptr = &x.

Pointers are particular nice for pointing for dynamic groups of data (aka dynamic arrays, matrices). Check out the malloc, realloc, and free functions to see how you allocate dynamic memory.

Operations here:

int *ptr = malloc (sizeof(int) * 10); //allocates a dynamic array of 10 ints pointed to by ptr.
ptr = realloc (ptr,sizeof(int) * 20); //reallocates the array to that of 20 ints
ptr[3] = 10; //sets the 4th element of the array to 10 (c arrays start at 0!)
*(ptr+3) = //same as the line above.
free(ptr); // Frees up the memory and points ptr to NULL.

Some good rules to follow:
  • Initialize pointers to NULL. If you are going to be dynamically allocating the memory for a pointer later then initialize it to NULL now. This way you don't have a pointer pointing to something random that can cause weird bugs if you accidentally use it pointing to that random location.
  • Always always always check to make sure a pointer doesn't equal NULL before you use it. If you follow this rule and the rule above you will save yourself a lot of grief.
  • Be careful when going through dynamic arrays. Make sure you keep good track of the size. All too often you see cases where people try and load a spot in the array they haven't allocated memory for and instead of causing the program to exit it causes seemingly unexplainable weirdness in your programs operation. This also leads to the famous buffer overflows in programs that plague security.

This is pretty basic but I hope it helps you out.
 
Old 02-29-2004, 12:43 PM   #6
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
Hey HW is due!
 
Old 02-29-2004, 05:59 PM   #7
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
wow the guy said he was a newbie and didnt knwo pointers maybe you guys should have given him something a little more simple like using pointers to reverse a string or something
 
Old 02-29-2004, 06:08 PM   #8
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
Quote:
Originally posted by ElementNine
wow the guy said he was a newbie and didnt knwo pointers maybe you guys should have given him something a little more simple like using pointers to reverse a string or something
I kinda expected him to come back and ask some more questions but I guess we( maybe I) scared him away. Anyway I am prepared to give him the solution if he asks for it.
 
Old 03-01-2004, 12:20 AM   #9
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hahah hey guys,

yeah im still trying to get the hang of pointers

im learning from a book called "Practical C++" by Rob McGregor

i think i understand basically how to use pointers:

int num;
int *pnum = #

// this is the part that confuses me:

//somtimes the book shows this:
pnum = 5;

// and sometimes:
*pnum = 5;

where it has the dereferencing operator and sometimes it doesnt lol

and also when pointing to an array or a structure....

yeah thanks for the assignments for me to do, but i dont think i can do that stuff yet haha .. meh.....

- just curious, but how old is everyone here and when they started programming?

thanks guys
 
Old 03-01-2004, 01:06 AM   #10
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 31
pointers in C can be a stumbling-block for newbie programmers. i suggest you read Ted Jensen's tutorial. i think his site is down so i uploaded the PDF: http://users.adelphia.net/~mksad/pointers.pdf

soon you'll see, pointers are fun.
 
Old 03-01-2004, 04:14 AM   #11
Chris Weimer
Member
 
Registered: Jan 2004
Location: http://www.neonostalgia.com/
Distribution: Slackware 12.1
Posts: 81

Rep: Reputation: 15
here's the tutorial without pdf version
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
 
Old 03-01-2004, 07:45 PM   #12
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
hrm the non pdf version isnt working lol
 
Old 03-01-2004, 08:30 PM   #13
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
After you understand the pointers, the only way to start from there is assign yourself a project starting from a simple and fun one and do it. Do the projects as much as you can and you will be learning while doing it and get the experience you cannot get from books and you will find yourself improve a lot at the end of the day. Most importantly you will get the confidence.
 
Old 03-01-2004, 09:15 PM   #14
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
ah thanks moeminhtun

yeah thats a good idea for giving myself projects
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I wanna learn Solaris-Newbie....give me a tutorial indian Solaris / OpenSolaris 2 04-25-2007 11:24 PM
The best way to learn? iz3r Programming 7 02-06-2005 11:00 PM
Do you really need to learn C before C++? qcoder Programming 31 12-04-2003 10:10 AM
I would like to learn how to do it tournesol Linux From Scratch 2 10-07-2002 10:07 PM
How to learn more? R2RO Linux - Software 5 08-31-2002 08:15 PM


All times are GMT -5. The time now is 04:51 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration