Very Basic Questions

General discussion about C/C++

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Very Basic Questions

Postby zealiff » Thu Jan 28, 2010 3:07 pm

Hi all. I have a VERY basic question regarding parenthesis.

1. What does the parenthesis mean after variables? For Example:
Code: Select all
int Main()


or

Code: Select all
void Something (void)


I don't understand when and why you need these. Also, for the above line of code, why do you need void inside the ().
zealiff
 
Posts: 3
Joined: Thu Jan 28, 2010 10:16 am

Re: Very Basic Questions

Postby Alvaro » Thu Jan 28, 2010 6:26 pm

I'm assuming you are trying to learn C++.

zealiff wrote:Hi all. I have a VERY basic question regarding parenthesis.

1. What does the parenthesis mean after variables? For Example:
Code: Select all
int Main()


or

Code: Select all
void Something (void)


I don't understand when and why you need these.

http://www.cplusplus.com/doc/tutorial/functions/

Also, for the above line of code, why do you need void inside the ().

In C++ you don't.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Very Basic Questions

Postby zealiff » Mon Feb 01, 2010 10:36 am

Ok next question. This is about Pointers.

If you have this:
Code: Select all
andy = 25;        // andy == 25
ted = &andy;    // ted == memory address
beth = *ted;     // beth == 25


Why would you need to get the memory address? Please don't point me to a website, give me the kindergarden answer.

Thanks
zealiff
 
Posts: 3
Joined: Thu Jan 28, 2010 10:16 am

Re: Very Basic Questions

Postby Alvaro » Mon Feb 01, 2010 10:38 am

The code would be easier to understand if you declare the types of the variables you use. And I don't understand the question.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Very Basic Questions

Postby zealiff » Mon Feb 01, 2010 10:45 am

It's just an example.

Again, why would you need to get the address of a variable stored in memory?

If you scratching your head, please re-read the title of this thread... :)
zealiff
 
Posts: 3
Joined: Thu Jan 28, 2010 10:16 am

Re: Very Basic Questions

Postby Alvaro » Mon Feb 01, 2010 10:54 am

For instance, you may want to have a function that changes the value of your variable.

Code: Select all
#include <iostream>

void add_five(int *x) {
  *x += 5;
}

int main() {
  int i=0;
  add_five(&i);
  std::cout << i << '\n';
}


You'll see that this is how scanf() is used, for instance.

In C++ you can achieve the same thing using references, but in C you couldn't. It can be argued that passing a pointer is more clear.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Very Basic Questions

Postby dennism12a » Sun Jun 13, 2010 5:35 am

In C brackets [] have a higher precedence than the asterisk *
User avatar
dennism12a
 
Posts: 2
Joined: Fri Jun 11, 2010 6:56 am

Re: Very Basic Questions

Postby Sunny » Mon Jul 26, 2010 3:07 pm

ddexxters75 wrote:It's just an example.
Again, why would you need to get the address of a variable stored in memory?If you scratching your head, please re-read the title of this thread..


How Alvaro said: „to change the value of a variable with a function“

Maybe it isn't clear for you yet, but after a while you will understand the point of pointers!

try this:

Code: Select all
#include <iostream>

void addFive_byvalue(int x) { // here is 'x' just a copy of the 'x' that we pass. programmer would say: „call by value“
    x = 4;
}
void addFive_byreference(int* x) { // here is 'x' a ptr which will contain the address of our 'x' variable. programmer would say: „call by reference“
    x = 4;
}

int main() {
    int x = 0;
    addFive_byvalue(x);
    std::cout << "by value: " << x << std::endl; // 0
    addFive_byreference(&x); // here would the function change the variable 'x'
    std::cout << "by reference: " << x << std::endl; // 4
}
User avatar
Sunny
 
Posts: 6
Joined: Thu Jul 22, 2010 5:01 pm

Re: Very Basic Questions

Postby JamsPeter » Wed Oct 20, 2010 6:05 am

zealiff wrote:Hi all. I have a VERY basic question regarding parenthesis.

1. What does the parenthesis mean after variables? For Example:
Code: Select all
int Main()


or

Code: Select all
void Something (void)


I don't understand when and why you need these. Also, for the above line of code, why do you need void inside the ().


Parenthesis defines that this is function or method. Only function consumes parenthesis.
Additional, Void in side the parenthesis describe function can accepts Parameter value. You can also write without void.
Last edited by JamsPeter on Thu Oct 21, 2010 1:58 am, edited 1 time in total.
JamsPeter
 
Posts: 3
Joined: Wed Oct 20, 2010 5:49 am

Re: Very Basic Questions

Postby evelyn512 » Fri Nov 04, 2011 7:05 am

You posted a very helpful information. I could finally fix my challenge. Great work guys:)
evelyn512
 
Posts: 1
Joined: Fri Nov 04, 2011 6:54 am


Return to General

Who is online

Users browsing this forum: No registered users and 0 guests