Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 6th, 2008, 12:38 AM   #1
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 12
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Some Beginner Questions

I am learning out to program just for a hobby.. to learn how to program I am reading C++ without fear. If there is a better book to ready what is it?? but as for my programming questions,,

What exactly does Void mean?

What does it mean when you declare something with Void type?
example
C++ Syntax (Toggle Plain Text)
  1. void draw_a_card;

What does it mean when you
c++ Syntax (Toggle Plain Text)
  1. return void;
?

Whats the difference between NULL and 0?

and the last thing is
What is the astrix mean in this line of code?
c++ Syntax (Toggle Plain Text)
  1. char *suit [4] = stuff.....

those are just a few things that i dont understand and any help on this would be greatly appreciated. thanks
ChevyCowboy15 is offline   Reply With Quote
Old May 6th, 2008, 1:27 AM   #2
hammerhead
Newbie
 
hammerhead's Avatar
 
Join Date: Apr 2008
Location: India
Posts: 3
Rep Power: 0 hammerhead is on a distinguished road
Re: Some Beginner Questions

>>What does it mean when you declare something with Void type?
I have only seen pointers of void being declared for example
C++ Syntax (Toggle Plain Text)
  1. int a;
  2. char b;
  3. float c;
  4. void *ptr_void;
  5.  
  6. ptr_void=&a;
  7. ptr_void=&b;
  8. ptr_void=&c;
in the above case, void can point to any data type. Maybe some of the more experienced members can help you out with your specific case.

return void; would mean returning nothing. You can simply use return

>>Whats the difference between NULL and 0?
Sorry but I do not know the answer.

What is the astrix mean in this line of code?
The asterisk is used to declare pointers in C++. In your case you have created an array of pointers of type char.
hammerhead is offline   Reply With Quote
Old May 6th, 2008, 2:15 AM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
Re: Some Beginner Questions

Quote:
Originally Posted by ChevyCowboy15
What exactly does Void mean?
The term Void has no meaning in C or C++. However, void (note the lowercase 'v') refers to something that has no type. For example, a function returning void actually means the function doesn't return any value at all. When a return statement is executed inside the function body, it just ends, and program control returns to the calling function.

In the context of pointers, a void pointer is just a pointer that can be safely cast to any type. For example, say you use malloc() to allocate memory for an array of int elements; malloc() will return a void pointer to the start of the block, or NULL if it failed. However, if you're using C++, this is mainly needed for interfacing with C code. Most C++ prgrammers will use new and delete instead of malloc() and free() for memory allocation and decallocation, since they are more type-safe.

Quote:
Originally Posted by ChevyCowboy15
What does it mean when you declare something with Void type?
If it's a function, it means the function returns nothing (in other words, has no return value). If it's a pointer, it means it can safely be cast to any type. You cannot declare a plain variable to be of void type, but can declare functions, pointers, or function pointers (since these are just a subset of pointers) this way.

Quote:
Originally Posted by ChevyCowboy15
Whats the difference between NULL and 0?
Nothing, and everything. NULL is a pointer constant, and simply equates to an invalid poitner value. In other words, a pointer that is not pointing to any valid memory location (see pointers explanation below) is NULL. You use the constant NULL to test a pointer before using it, and most functions that return a pointer will return NULL if they failed. If you initialize a pointer with a value of zero, this is synonymous with initializing it with NULL, even if the machine's internal representation of an invalid pointer is not an all-bits-zero value.
Quote:
Originally Posted by ChevyCowboy15
What is the astrix mean in this line of code?
That declares a pointer. A pointer is simply a data type that 'points at' something in memory. Typically this will be a variable or array, but you can have pointers to functions as well.

With computers, everything that exists in memory must, by definition, exist at a specific location in memory. Picture the total memory available to your program as one big array of bytes (ie, type char). Each element will have an index, from zero on up to the number of bytes available minus one. Thus, for 256 bytes of space, the numbering goes from 0 to 255. This number that specifies the location is an 'address', and just as your street address identifies your building, the address of a piece of code or data identifies where it is.

The reason you need to declare a pointer as being of a specific type is so that the compiler can generate appropriate code. Say you have an int variable, and let's say this takes up four bytes (this is common on modern systems). Now let's say you store this variable's address in a pointer, and then tell the compiler to do something to the location where the pointer is pointing, such as 'increment the pointed-at location by one'. In order to do this, the compiler needs to know how 'big' the pointed-at item is, as well as what machine instructions to use (incrementing an integer is different that incrementing a floating-point value, for example). This is why pointers have a type. A second reason that pointers have a type is so the compiler can do proper error checking, such as ensuring you pass a pointer of the appropriate type to a function.

Pointers have many uses in C, and are quite common in C++ as well. Here are some examples of why you will use pointers:
  1. Say you want to create an array whose size is determined at run time, rather than in advance (at compile time). You can use malloc() or new to allocate a block of memory of the appropriate size, and obtain a pointer to the start of the block.
  2. Imagine you want a function to be able to return more than one value. You can only directly return one, so you try passing the second value, and have the function 'fill it in' with a second return value. This will not work if you pass a plain variable, since C and C++ pass a copy (this copy is local to the called function). Passing a pointer to the variable allows the called function to fill it in indirectly. C++ (but not C) has a new 'reference type' to make this a little easier, but in reality, it generates equivalent (and often identical) machine instructions.
  3. Let's say you have a function that needs to call a second function, but you don't know exactly what this second function is at compile time. For example, let's say you have functions to draw pixels, and which function you call depends on which graphics mode you're currently in. You could have conditional logic to test the current mode, and call the appropriate function, or you could use function pointers. When initializing the display, simply load the function pointer with the address of the appropriate function, and call it indirectly.
There are many more uses for pointers, but these are a few. Because they're so important to C and C++ programming, it's a very good idea to learn about them. However, they can be a bit tricky, so don't worry if you don't understand them right away. Most people take a while before they are comfortable with using pointers.

Having said all that, the asterisk actually has three meanings. First, as you know, is multiplication. The second, as I explained above, is for declaring a pointer variable. The third is used to 'dereference' a pointer. This sounds complicated, but it's actually pretty simple. If a pointer holds the address of something, then the value of the pointer is the address, and dereferencing the pointer yields the value of what is pointed to, or allows you to assign to the memory location.
C++ Syntax (Toggle Plain Text)
  1. int x = 1234; // x now holds the value 4321
  2. int *p = &x; // this says 'load p with the address of x',
  3. // with '&' being the address-of operator
  4. int y = *p; // y now holds the value 1234
  5. *p = 4321; // x now holds the value 4321
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old May 6th, 2008, 12:49 PM   #4
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 12
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Re: Some Beginner Questions

thanks for the help... i just goit one more questions and that is

why would you want to void type something?

like in that example why would you do that?? i thought that everything had to either be a char, string, int of some type, double and so on... why would you declare something void
ChevyCowboy15 is offline   Reply With Quote
Old May 6th, 2008, 2:49 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
Re: Some Beginner Questions

Quote:
Originally Posted by ChevyCowboy15
like in that example why would you do that?? i thought that everything had to either be a char, string, int of some type, double and so on... why would you declare something void
The only things that have type void are functions or pointers. A function of type void simply means the function has no return value. A parameter list can also be void, but this is not a case of having a type; rather, it's an indicator that there is no parameter.

Say you have two functions. One calculates the square of an integer, and the other clears the screen. Now, the first is going to need to return a value, whereas the second is not (thus, its return type is void). The first function is also going to need a parameter (the number to calculate the square of), whereas the second is not going to need a parameter in this example; its parameter list will therefore be a simple void which means it takes no parameters. The function prototypes might look like this:
C++ Syntax (Toggle Plain Text)
  1. int square(int value); // returns the square of 'value'
  2. void clearScreen(void); // clears the screen
If a function is not type void, you can use it in an expression:
C++ Syntax (Toggle Plain Text)
  1. int x = square(37);
If it is type void, you cannot:
C++ Syntax (Toggle Plain Text)
  1. int y = clearScreen(); // error
The example above is an error because clearScreen() does not return a value. If you've been exposed to other languages, you might be familiar with a distinction between functions (that return a value), and procedures or subroutines (which do not). In these languages, the clearScreen() function above would instead be called a procedure or subroutine, to indicate it has no return value. In C and C++, however, they are all functions; whether they return a value or not affects how you use them, but not what they are.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old May 6th, 2008, 6:00 PM   #6
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 12
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Re: Some Beginner Questions

ok now i understand more about.. Thank You!!!
ChevyCowboy15 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VoIP Questions crawforddavid2006 Coder's Corner Lounge 8 Jan 16th, 2008 1:53 PM
Template Questions King C++ 3 Feb 3rd, 2007 7:30 PM
hardware questions programmingnoob Coder's Corner Lounge 28 Aug 8th, 2006 8:04 PM
ucf cs student has some questions raspberryh Coder's Corner Lounge 33 Sep 12th, 2005 2:49 PM
n00b questions..A thread on dumb questions. Cipher Coder's Corner Lounge 12 Apr 12th, 2005 9:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC