1.The arguments in a function call must match the arguments in
the function's header
a)only when the function call is in a different file from the function body.
b)in number, order, and variable name.
c)in number and order only.
d)in number, order, and type.
e)They don't have to match the header, only the function prototype.
2.To use I/O redirection to read from a file you need to:
a.declare a file variable in your program and openit
b.use the full path name with cin (a:\file1.dat.cin %26gt;%26gt;)
c.run your program from dos and add the file to be read from with the correct operator ( myprog %26lt; file1.dat)
d.any of the above will work
3.If you try to explicitly open an ifstream or ofstream file, how can you tell if the file opened successfully?
a.use the fail() function (method)
b.a return code of 1 indicates success
c.a pointer to a buffer that will receive data will be null if it fails
d.there is no way to tell other than to try to do input of output
e.a return code of 0 for success
C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?
1-d
2-b
3-c
Please select best answer if all are correct
survey monkey
Tuesday, July 14, 2009
C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?
#include %26lt;iostream%26gt;
int main(){
int* x;
int y = 4;
x = %26amp;y;
//is it correct to say something like x is a memory address and by putting %26amp;y, the compiler reads it as "take x, which is a variable that stores strictly a memory address and store the memory address which contains the content of the variable y"??
std::cout %26lt;%26lt; *x %26lt;%26lt; '\n';
int *z = x;
++*z;
//also for the above, int *z = x; is setting a variable z which strictly stores the address, to the address of x (which may or may contain data) and the ++*z is read by a compiler as (increment the value of the memory address z)
std::cout %26lt;%26lt; *z %26lt;%26lt; '\n';
return 0;
};
Once you declare a pointer such as int *ptr the rest of the block referring to ptr refers to the mem address and *ptr refers to the data at that address right?...where does %26amp;ptr fit into the picture?
Is what I am saying correct??? In your own words what does
int *ptr
ptr
%26amp;ptr
*ptr
all referring to?
Thanks!!
C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?
See if this makes some sense to you.
If I say;
int y = 5;
I created the variable y (at this point you have no idea where in memory this variable is) and it put the 5 in that location. So refering to y in a program is now like saying get what is in y (the 5) and do something with it.
std::cout %26lt;%26lt; y %26lt;%26lt; \n;
Now say you happen to know that the memory location used for y is 1000.
If I said:
int *x = 1000; // Note the compiler is probably going to complain about this because 1000 isn't a proper integer pointer, but this is just to get the idea.
Is creating the variable x. And putting the memory location 1000 in it. Now I want to not what is in x, but what is at the location 1000, so I "dereference the pointer x". Which is given by *x, or in other words give me the value of the integer at that location that is stored in x. So in this case since x is pointing the the same location as y, *x will give me 5.
std::cout %26lt;%26lt; *x %26lt;%26lt; '\n";
Now for %26amp;y. This will give me that address (memory location) of where y is storing its value. So without knowing where y is storing its value I can do it like this:
int *x = %26amp;y;
That creates x and sets it to the memory location where y is storing its interger value. And is the proper way to set the pointer x because I'm setting the integer pointer x to the memory location where y is storing its value.
Note that even though the syntax says;
int *x;
*x
These are actually not the same. The *x is just the way to declare a pointer to an integer.
Where as *x is tell it to given you the integer value from where x is pointing to.
Reply:int *ptr /*this delcairs a pointer of type int. Which means that the ptr can point to any data type flag as an int type. eg int or int[ ]*/
ptr = the address of something.
%26amp;ptr = this is the address of the pointer ptr , in terms of memory storage
*ptr = this "dereferences" the pointer and returns the contence of the memory address contain in ptr.
for example.....
int* pointer; // at this point the pointer is swinging... if you deref it you will segfault this is because it contains a random address some where on your computer.
int number = 100;
// now we want to store the the memory address of "number" so we say. "%26amp;" mean "the address of"
pointer = %26amp;number
// now we want to out put the number that the point points to.
so we dereference the pointer... which mean that we look at the contence of the pointer and convert it to an address, then we move to the address location and return that data to the calling line.
cout %26lt;%26lt; *pointer %26lt;%26lt; endl;
// if we wish to alter the data begin referenced we can do this
// "*" means dereference. or n simple terms move to down the pointer to the thing being pointed at.
(*pointer)++;
//or
*pointer = *pointer + 1;
anyway i hope this helps
int main(){
int* x;
int y = 4;
x = %26amp;y;
//is it correct to say something like x is a memory address and by putting %26amp;y, the compiler reads it as "take x, which is a variable that stores strictly a memory address and store the memory address which contains the content of the variable y"??
std::cout %26lt;%26lt; *x %26lt;%26lt; '\n';
int *z = x;
++*z;
//also for the above, int *z = x; is setting a variable z which strictly stores the address, to the address of x (which may or may contain data) and the ++*z is read by a compiler as (increment the value of the memory address z)
std::cout %26lt;%26lt; *z %26lt;%26lt; '\n';
return 0;
};
Once you declare a pointer such as int *ptr the rest of the block referring to ptr refers to the mem address and *ptr refers to the data at that address right?...where does %26amp;ptr fit into the picture?
Is what I am saying correct??? In your own words what does
int *ptr
ptr
%26amp;ptr
*ptr
all referring to?
Thanks!!
C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?
See if this makes some sense to you.
If I say;
int y = 5;
I created the variable y (at this point you have no idea where in memory this variable is) and it put the 5 in that location. So refering to y in a program is now like saying get what is in y (the 5) and do something with it.
std::cout %26lt;%26lt; y %26lt;%26lt; \n;
Now say you happen to know that the memory location used for y is 1000.
If I said:
int *x = 1000; // Note the compiler is probably going to complain about this because 1000 isn't a proper integer pointer, but this is just to get the idea.
Is creating the variable x. And putting the memory location 1000 in it. Now I want to not what is in x, but what is at the location 1000, so I "dereference the pointer x". Which is given by *x, or in other words give me the value of the integer at that location that is stored in x. So in this case since x is pointing the the same location as y, *x will give me 5.
std::cout %26lt;%26lt; *x %26lt;%26lt; '\n";
Now for %26amp;y. This will give me that address (memory location) of where y is storing its value. So without knowing where y is storing its value I can do it like this:
int *x = %26amp;y;
That creates x and sets it to the memory location where y is storing its interger value. And is the proper way to set the pointer x because I'm setting the integer pointer x to the memory location where y is storing its value.
Note that even though the syntax says;
int *x;
*x
These are actually not the same. The *x is just the way to declare a pointer to an integer.
Where as *x is tell it to given you the integer value from where x is pointing to.
Reply:int *ptr /*this delcairs a pointer of type int. Which means that the ptr can point to any data type flag as an int type. eg int or int[ ]*/
ptr = the address of something.
%26amp;ptr = this is the address of the pointer ptr , in terms of memory storage
*ptr = this "dereferences" the pointer and returns the contence of the memory address contain in ptr.
for example.....
int* pointer; // at this point the pointer is swinging... if you deref it you will segfault this is because it contains a random address some where on your computer.
int number = 100;
// now we want to store the the memory address of "number" so we say. "%26amp;" mean "the address of"
pointer = %26amp;number
// now we want to out put the number that the point points to.
so we dereference the pointer... which mean that we look at the contence of the pointer and convert it to an address, then we move to the address location and return that data to the calling line.
cout %26lt;%26lt; *pointer %26lt;%26lt; endl;
// if we wish to alter the data begin referenced we can do this
// "*" means dereference. or n simple terms move to down the pointer to the thing being pointed at.
(*pointer)++;
//or
*pointer = *pointer + 1;
anyway i hope this helps
Again c++ and pointers ...?
I try to learn the pointers and in the same time to create a GTA-SA trainer but i'm having problems ...
i use this code :
"
int main ()
{
int *pointer;
pointer = reinterpret_cast%26lt;int*%26gt;(0xB7CE50);
*pointer = 100;
getchar();
}
"
But i get this:
"Unhandled exception at 0x004113c8 in memory.exe: 0xC0000005: Access violation writing location 0x00b7ce50."
I also tried to do pointer = 0xB7CE50; but won't compile,can not convert from int to int* .
So what now?
Can anyone give me a good and working example?
PS:You can use inline ASM!
Thanks!!!
Again c++ and pointers ...?
The reason this is throwing an exception is because the memory location you are trying to write to 0x00B7CE50 is protected by the operating system.
The proper way to get memory for a pointer is to utilize the c++ opperator 'new'
try this:
int* pointer;
pointer = new int();
// in straight C this would be pointer = (int*)malloc(sizeof(int));
*pointer = 100;
i use this code :
"
int main ()
{
int *pointer;
pointer = reinterpret_cast%26lt;int*%26gt;(0xB7CE50);
*pointer = 100;
getchar();
}
"
But i get this:
"Unhandled exception at 0x004113c8 in memory.exe: 0xC0000005: Access violation writing location 0x00b7ce50."
I also tried to do pointer = 0xB7CE50; but won't compile,can not convert from int to int* .
So what now?
Can anyone give me a good and working example?
PS:You can use inline ASM!
Thanks!!!
Again c++ and pointers ...?
The reason this is throwing an exception is because the memory location you are trying to write to 0x00B7CE50 is protected by the operating system.
The proper way to get memory for a pointer is to utilize the c++ opperator 'new'
try this:
int* pointer;
pointer = new int();
// in straight C this would be pointer = (int*)malloc(sizeof(int));
*pointer = 100;
What are examples of output statements for arrays and pointers in C++?
I am trying to learn pointers and arrays for C++ class. I am using MS Visual Studio 2005. I can initialize, I can set it. But I can't get it to print to the screen. Any beginner help is welcomed!
What are examples of output statements for arrays and pointers in C++?
You had not said whether you want to print address your pointer points to or data placed at this address.
In plain C:
...
int n = 0;
int *p = %26amp;n;
printf( "At address %p we have value %d\n", p, *p );
Reply:#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int arr[5] : {1,5,3,9,4};
cout %26lt;%26lt; arr[4]; //should print "9"
return 0;
}
Reply:use a for loop
like for(int i=0;i%26lt; 100;i++)
cout %26lt;%26lt; a[i] %26lt;%26lt; endl;
What are examples of output statements for arrays and pointers in C++?
You had not said whether you want to print address your pointer points to or data placed at this address.
In plain C:
...
int n = 0;
int *p = %26amp;n;
printf( "At address %p we have value %d\n", p, *p );
Reply:#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int arr[5] : {1,5,3,9,4};
cout %26lt;%26lt; arr[4]; //should print "9"
return 0;
}
Reply:use a for loop
like for(int i=0;i%26lt; 100;i++)
cout %26lt;%26lt; a[i] %26lt;%26lt; endl;
What is the necessity of pointers in C language explicitly or implicitly?
interview question asked in infosys and can't we have C language without the pointers??
What is the necessity of pointers in C language explicitly or implicitly?
Pointers are needed for:
Reference parameters in functions (call by reference)
For functions with variable arguments (like printf)
Dynamic data structures (like trees, lists and dynamically allocated arrays)
As function pointers (callbacks) used mainly in libraries for code reuse (see qsort function)
For system programing (low level I/O).
Without pointers C would be something like the primitive basics used in home computers.
There are alternatives to pointers called references, but those are only used in modern langauges like C# or Java.
Reply:Possibility of both are exist because you can create reference of pointer as void (pointer i.e., void *) then explicitly give the address of any type...
in contrast of implicitly you can create any type data pointer and which will hold the address of related type...
so it depends on your need. I hope it will help full for you.
if any question in more precise way..
bye
online survey
What is the necessity of pointers in C language explicitly or implicitly?
Pointers are needed for:
Reference parameters in functions (call by reference)
For functions with variable arguments (like printf)
Dynamic data structures (like trees, lists and dynamically allocated arrays)
As function pointers (callbacks) used mainly in libraries for code reuse (see qsort function)
For system programing (low level I/O).
Without pointers C would be something like the primitive basics used in home computers.
There are alternatives to pointers called references, but those are only used in modern langauges like C# or Java.
Reply:Possibility of both are exist because you can create reference of pointer as void (pointer i.e., void *) then explicitly give the address of any type...
in contrast of implicitly you can create any type data pointer and which will hold the address of related type...
so it depends on your need. I hope it will help full for you.
if any question in more precise way..
bye
online survey
Sample programs of POINTERS in C.?
sample programs of POINTERS in C.
Sample programs of POINTERS in C.?
#include %26lt;iostream%26gt;
using namespace std;
int main(int argc, char **argv)
{
cout %26lt;%26lt; "The name of this program is: " %26lt;%26lt; *argv %26lt;%26lt; endl;
}
Reply:yes
Reply:AND !!
Sample programs of POINTERS in C.?
#include %26lt;iostream%26gt;
using namespace std;
int main(int argc, char **argv)
{
cout %26lt;%26lt; "The name of this program is: " %26lt;%26lt; *argv %26lt;%26lt; endl;
}
Reply:yes
Reply:AND !!
Tell me about pointers in c.and some exaples and use of ponters?
i am a student. i am doing bca from ignou in this course i am reading pointers in c. so there any one who can help in understanding the pointers with some examples. or you can tell me about sites that can help in doing this course succsessfully or any one that done or purcuing bca from ignou contact me
warm regards
yogesh tyagi
gurgaon(122001)
Tell me about pointers in c.and some exaples and use of ponters?
buy "understanding pointers in c" by 'Yashwant Kanetkar' dude................
warm regards
yogesh tyagi
gurgaon(122001)
Tell me about pointers in c.and some exaples and use of ponters?
buy "understanding pointers in c" by 'Yashwant Kanetkar' dude................
Subscribe to:
Posts (Atom)