![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0
![]() |
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)
What does it mean when you c++ Syntax (Toggle Plain Text)
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)
those are just a few things that i dont understand and any help on this would be greatly appreciated. thanks |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2008
Location: India
Posts: 3
Rep Power: 0
![]() |
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)
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. |
|
|
|
|
|
#3 | ||||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,009
Rep Power: 5
![]() |
Re: Some Beginner Questions
Quote:
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:
void type, but can declare functions, pointers, or function pointers (since these are just a subset of pointers) this way.Quote:
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:
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:
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)
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
||||
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,009
Rep Power: 5
![]() |
Re: Some Beginner Questions
Quote:
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)
C++ Syntax (Toggle Plain Text)
C++ Syntax (Toggle Plain Text)
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0
![]() |
Re: Some Beginner Questions
ok now i understand more about.. Thank You!!!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |