There are several restrictions to using pointers in C#. I've included a link to the MSDN section on using pointers in C# below.
I would encourage the use of objects instead of pointers if you can get away with it. Pointers are considered "unsafe" types, and must be included in trusted assemblies (if compiled into an assembly) to be accessed.
I believe pointers in C# (or .NET for that matter) are allocated on the heap, unless you call stackalloc to explicitly allocate them on the stack. They are declared with an * after the type declaration for multi-pointer declarations, or an * before the variable name for single pointer declarations. Two asterisks indicate a pointer to a pointer.
Also, any methods where a pointer is declared/used must be declared with an "unsafe" declaration before the method declaration.
You can obtain further information at the URL below.
How can I use pointers in C# programming?
The design of C# is to avoid the use of pointers. "Managed Code" is what it's called. For the most part you shouldn't need pointers in C# but you still have the ability to use pointers in C# but its strongly discouraged.
Reply:Hi
Pointer is a powerful tool in 'C', it is used to know the memory address of a variable/identifier/value
Pointers are also helpful in transfer more than one values/variables/identifiers from main program/function to another function
Tuesday, July 14, 2009
How can I use pointers in C#.net?
What is unsafe mode?
How can I use pointers in C#.net?
You can use pointers just like in C++ although it is use is rare( perhaps only for dealing with older API's with Win32).
You can reference memory directly, add addresses, pass by reference etc.
All operations with pointers need to be within unsafe mode blocks. Pointer operations are dangerous because you can modify or delete memory of other variable/space, for this the compiler requires you to mark areas where you use them as unsafe.
The best explanation you can find in MSDN.
How can I use pointers in C#.net?
You can use pointers just like in C++ although it is use is rare( perhaps only for dealing with older API's with Win32).
You can reference memory directly, add addresses, pass by reference etc.
All operations with pointers need to be within unsafe mode blocks. Pointer operations are dangerous because you can modify or delete memory of other variable/space, for this the compiler requires you to mark areas where you use them as unsafe.
The best explanation you can find in MSDN.
How do you use pointers in C#?
I want to have a list of items where an item points to the next item.
How do you use pointers in C#?
You don't need a pointer to do this, only a reference (which works the same way.)
Define a class
Add an instance variable nextItem of the same type as the class.
You can then tell each class what the next item in the structure is. You can also have a prevIitem variable to get a double-linked list.
Reply:Sorry, C# doesn't support pointers. You probably need to use the List collection. I havn't used it so I'm not sure. But I do know the pointers where one of the main features removed from C#.
survey for money
How do you use pointers in C#?
You don't need a pointer to do this, only a reference (which works the same way.)
Define a class
Add an instance variable nextItem of the same type as the class.
You can then tell each class what the next item in the structure is. You can also have a prevIitem variable to get a double-linked list.
Reply:Sorry, C# doesn't support pointers. You probably need to use the List collection. I havn't used it so I'm not sure. But I do know the pointers where one of the main features removed from C#.
survey for money
Can u tell about pointers in c++?
give an example program using class with resolution operator
Can u tell about pointers in c++?
Pointers are a type of special variable that store the address of the ordinary data type variables rather than the variable itself. They are also of types int,char, etc to store addresses of variable of type int,char...
eg:
suppose intx=3;
the variable x gets stored at a memory location 1001h
the %26amp; operator defines the address of a variable
now declare an pointer variable of type int
int *y;
y=%26amp;x;
here the address of x gets stored in y.
cout%26lt;%26lt;y;
the result will be like 1001.
this is the simplest way i can give you the concept of pointers but it is a huge topic to be understood completely.
bye
Reply:http://www.google.com/search?q=c%2B%2B+p...
Reply:MyClass *obj = new MyClass();
obj-%26gt;doit();
How's that? -%26gt; is it.
Can u tell about pointers in c++?
Pointers are a type of special variable that store the address of the ordinary data type variables rather than the variable itself. They are also of types int,char, etc to store addresses of variable of type int,char...
eg:
suppose intx=3;
the variable x gets stored at a memory location 1001h
the %26amp; operator defines the address of a variable
now declare an pointer variable of type int
int *y;
y=%26amp;x;
here the address of x gets stored in y.
cout%26lt;%26lt;y;
the result will be like 1001.
this is the simplest way i can give you the concept of pointers but it is a huge topic to be understood completely.
bye
Reply:http://www.google.com/search?q=c%2B%2B+p...
Reply:MyClass *obj = new MyClass();
obj-%26gt;doit();
How's that? -%26gt; is it.
I need a code for pascal's triangle in c++?
without using pointers (simple c++ lang)
the output should be: 1
1 1
1 2 1
1 3 3 1
and follows...........
pls help me out .....
I need a code for pascal's triangle in c++?
search google
the output should be: 1
1 1
1 2 1
1 3 3 1
and follows...........
pls help me out .....
I need a code for pascal's triangle in c++?
search google
Why we need pointers in C?
A pointer is the address of a variable rather than the content of it. For example try to figure out the difference between the "the third drawer" (that is, where it is located) and "the content of the third drawer" (what is inside it).
In C algorithms, there are times when we know where our variable is located, and we want to do some arithmetic or manipulation on it. In these cases, the address of the variable (a pointer to it) is more useful than the value of it. More technically, it is named call by reference. As a simple example imagine the difference between
void inc1 (int x)
{
x++ ;
}
and
void inc2 (int * x)
{
(*x)++ ;
}
If we call inc1 (t), the value of t (and int variable) is copied into x, and then the value of x is incremented. But t has not changed. It’s much like I photocopy a page and I give the copy to you and you write something new on your own copy. It is the normal call-by-value function call.
But if we call inc2(%26amp;t), the address (and not the content) of t is copied into pointer x. then the content of x is incremented. Because x has the address of t, it would mean that the content of t is incremented. It’s much like I tell you where the original page is located, and you go to the page, and write something new on it. It is call-by-reference function call.
--------------------------------------...
I added this comment later. You could mean why we need pointers in C and C++ but we don't need them in languages like C# and VB?
Actually, pointers are used in VB and C#, but this use is not as transparent to the programmer as it is in C and C++. Reference as called in VB and C# is a disguised look of pointer with more limited capabilities as comapared to its capabilities in C and C++. Although pointers provide a more powerful capability, it has some potential danger and problem which could be difficult to find. Reference is quite safe, and is adequate for alomost all the situations.
Why we need pointers in C?
Pointers alows you to do low level programming efficiently from C.
Reply:Because they are alot better then using arrays.
Pointers take up less space in memory and can be resized.
However sometimes it just better to use a array if the list is small enough.
Reply:Because of the way programming works in general.
When you develop an application you need to use memory resources. C being and older high level language gives you alot of low level flexibility and control. When you are using a language like C#, Java, Python and the like, they are actually using pointers for non value types.
But C gives you the ability to allocate RAM for your application and you can set a pointer to point to that area, and use it anyway you want, as a string*, char*, int*.
It is not a matter of needing pointers in C, pointers are needed in any good programming language, but C makes the concept of a pointer alot more apparent and gives you the choice to use value types or pointer types.
I would ask ... why would any language need complex value types like structs lol.
In C algorithms, there are times when we know where our variable is located, and we want to do some arithmetic or manipulation on it. In these cases, the address of the variable (a pointer to it) is more useful than the value of it. More technically, it is named call by reference. As a simple example imagine the difference between
void inc1 (int x)
{
x++ ;
}
and
void inc2 (int * x)
{
(*x)++ ;
}
If we call inc1 (t), the value of t (and int variable) is copied into x, and then the value of x is incremented. But t has not changed. It’s much like I photocopy a page and I give the copy to you and you write something new on your own copy. It is the normal call-by-value function call.
But if we call inc2(%26amp;t), the address (and not the content) of t is copied into pointer x. then the content of x is incremented. Because x has the address of t, it would mean that the content of t is incremented. It’s much like I tell you where the original page is located, and you go to the page, and write something new on it. It is call-by-reference function call.
--------------------------------------...
I added this comment later. You could mean why we need pointers in C and C++ but we don't need them in languages like C# and VB?
Actually, pointers are used in VB and C#, but this use is not as transparent to the programmer as it is in C and C++. Reference as called in VB and C# is a disguised look of pointer with more limited capabilities as comapared to its capabilities in C and C++. Although pointers provide a more powerful capability, it has some potential danger and problem which could be difficult to find. Reference is quite safe, and is adequate for alomost all the situations.
Why we need pointers in C?
Pointers alows you to do low level programming efficiently from C.
Reply:Because they are alot better then using arrays.
Pointers take up less space in memory and can be resized.
However sometimes it just better to use a array if the list is small enough.
Reply:Because of the way programming works in general.
When you develop an application you need to use memory resources. C being and older high level language gives you alot of low level flexibility and control. When you are using a language like C#, Java, Python and the like, they are actually using pointers for non value types.
But C gives you the ability to allocate RAM for your application and you can set a pointer to point to that area, and use it anyway you want, as a string*, char*, int*.
It is not a matter of needing pointers in C, pointers are needed in any good programming language, but C makes the concept of a pointer alot more apparent and gives you the choice to use value types or pointer types.
I would ask ... why would any language need complex value types like structs lol.
How do pointers in C++ save memory space and how does memory work?
A space in memory holds some value of information that your program has specified it will need at some time. So if you have already declared a space and need to reference the value of that space, you can use a pointer, instead of creating a duplicate space in memory. Your pointer just keeps reference of the location in memory where your original information is stored. So it saves space by not keeping the entire length of your information in two different places in memory.
How do pointers in C++ save memory space and how does memory work?
Think about a cabinet with 10 x 10 drawers. A pointer is a reference to the drawer. You will just need two digits to specify the location of any item. Now your item may be as big as you want, and with some imagination, each drawer could hold another cabinet with each one 10x10 drawers... And you get a pointer of pointer...
survey questions
How do pointers in C++ save memory space and how does memory work?
Think about a cabinet with 10 x 10 drawers. A pointer is a reference to the drawer. You will just need two digits to specify the location of any item. Now your item may be as big as you want, and with some imagination, each drawer could hold another cabinet with each one 10x10 drawers... And you get a pointer of pointer...
survey questions
NEED URGENT HELP IN C programming?
PLS tell how to calculate days between dates using structure pointers in C
NEED URGENT HELP IN C programming?
/* difftime example */
#include %26lt;stdio.h%26gt;
#include %26lt;time.h%26gt;
int main ()
{
time_t start,end;
char szInput [256];
double dif;
time (%26amp;start);
printf ("Please, enter your name: ");
gets (szInput);
time (%26amp;end);
dif = difftime (end,start);
printf ("Hi %s.\n", szInput);
printf ("It took you %.2lf seconds to type your name.\n", dif );
return 0;
}
Reply:read in two dates.
from the earlier date, keep adding one day, until you reach the other date.
Look at how many days you had to add.
you got what you wanted ;)
NEED URGENT HELP IN C programming?
/* difftime example */
#include %26lt;stdio.h%26gt;
#include %26lt;time.h%26gt;
int main ()
{
time_t start,end;
char szInput [256];
double dif;
time (%26amp;start);
printf ("Please, enter your name: ");
gets (szInput);
time (%26amp;end);
dif = difftime (end,start);
printf ("Hi %s.\n", szInput);
printf ("It took you %.2lf seconds to type your name.\n", dif );
return 0;
}
Reply:read in two dates.
from the earlier date, keep adding one day, until you reach the other date.
Look at how many days you had to add.
you got what you wanted ;)
Questions relatewd to c & java?
about pointers in c.
operators in java
Questions relatewd to c %26amp; java?
pointers are ref addresses
operators are symbols that rep a operation ie - is minus
hope this helps :)
Reply:http://www.pekiyi.150m.com
java and c language pages.
Reply:point it can store the other address of other object.
Reply:lol
Reply:Go to this site to get details of pointers in C.
http://www.cs.cf.ac.uk/Dave/C/node10.htm...
operators in java
Questions relatewd to c %26amp; java?
pointers are ref addresses
operators are symbols that rep a operation ie - is minus
hope this helps :)
Reply:http://www.pekiyi.150m.com
java and c language pages.
Reply:point it can store the other address of other object.
Reply:lol
Reply:Go to this site to get details of pointers in C.
http://www.cs.cf.ac.uk/Dave/C/node10.htm...
About pointers in C...?
i am confused between these two..
int *s,p=1;
s=%26amp;p;
s=p;
if s=%26amp;p points the address of p by s
how about s=p?
what is the difference between the two?
About pointers in C...?
s=%26amp;p
means you are assigning the address of the variable 'p' to the pointer variable 's'
and i think the second statement "s=p" will generate an error... coz you are trying to assign an int value to an int pointer.
Reply:So, think of a computer's memory as a long tape, and an address as a position on that tape. A pointer is simply an address contained in a variable that tells the computer to go to the position on the tape that's specified in the variable, got it? Now, by going %26amp;p, you're looking for where p is on that 'tape' so the computer can find it and change it's value later. When you store p into s, you're storing the contents of p into something that's supposed to only hold an address. So, when the computer tries to dereference the pointer, it ends up looking for this value that could be anywhere on the 'tape', but doesn't refer to anything specifically, which is very bad. The only time you'd see something like s=p is if both were pointers, and you want p to refer to whatever s was referring to in the first place.
Reply:s = p will store what's in p into s, in other words s will contain 1. This is of course NOT what s is meant to have in it, and any decent C compiler should produce at least a warning message about this
int *s,p=1;
s=%26amp;p;
s=p;
if s=%26amp;p points the address of p by s
how about s=p?
what is the difference between the two?
About pointers in C...?
s=%26amp;p
means you are assigning the address of the variable 'p' to the pointer variable 's'
and i think the second statement "s=p" will generate an error... coz you are trying to assign an int value to an int pointer.
Reply:So, think of a computer's memory as a long tape, and an address as a position on that tape. A pointer is simply an address contained in a variable that tells the computer to go to the position on the tape that's specified in the variable, got it? Now, by going %26amp;p, you're looking for where p is on that 'tape' so the computer can find it and change it's value later. When you store p into s, you're storing the contents of p into something that's supposed to only hold an address. So, when the computer tries to dereference the pointer, it ends up looking for this value that could be anywhere on the 'tape', but doesn't refer to anything specifically, which is very bad. The only time you'd see something like s=p is if both were pointers, and you want p to refer to whatever s was referring to in the first place.
Reply:s = p will store what's in p into s, in other words s will contain 1. This is of course NOT what s is meant to have in it, and any decent C compiler should produce at least a warning message about this
Assigning pointers in C++?
I do not understand the following code
V * v = new V;
v-%26gt;i=8;
System::Console::WriteLine(v-%26gt;i);
pin_ptr%26lt;V%26gt; mv = %26amp;*v; %26lt;--- what does %26amp;*v means?
mv-%26gt;i = 7;
Assigning pointers in C++?
It appears that you took this example right from the MSDN documentation. Basically what this code is doing is declaring a pointer to the structure 'V', then it is setting the integer member 'i' to the value of 8.
The pin_ptr%26lt;V%26gt; mv = %26amp;*v; line is what creates a "pinned" pointer to the boxed value type. The "%26amp;*v" says to assign the address to the pinned pointer of the data which is pointed to by the pointer 'v'.
You can then change the value through the pinned pointer and your structure's member will be changed as well.
Reply:V *v=new V;
Creating a pointer object v, of a Class V.
v-%26gt;i=8;
Assigning value 8 to member variable i of object v.
%26amp;v is the address of pointer v.
surveys
V * v = new V;
v-%26gt;i=8;
System::Console::WriteLine(v-%26gt;i);
pin_ptr%26lt;V%26gt; mv = %26amp;*v; %26lt;--- what does %26amp;*v means?
mv-%26gt;i = 7;
Assigning pointers in C++?
It appears that you took this example right from the MSDN documentation. Basically what this code is doing is declaring a pointer to the structure 'V', then it is setting the integer member 'i' to the value of 8.
The pin_ptr%26lt;V%26gt; mv = %26amp;*v; line is what creates a "pinned" pointer to the boxed value type. The "%26amp;*v" says to assign the address to the pinned pointer of the data which is pointed to by the pointer 'v'.
You can then change the value through the pinned pointer and your structure's member will be changed as well.
Reply:V *v=new V;
Creating a pointer object v, of a Class V.
v-%26gt;i=8;
Assigning value 8 to member variable i of object v.
%26amp;v is the address of pointer v.
surveys
Questions related to c & java?
about pointers in c.
operators in java
Questions related to c %26amp; java?
lol
Reply:Yes, There is a concept in C called "Pointers" with which you can have variables that contain Address as their value.
And yes, There is a concept in every programming language, which is called "Operators", that refers to those special symbols used to perform a simple task. For example, the "++" Operator (without the quotes) is used to increase the amount of an integer value by 1.
What else do you want to know about them?
Reply:Go to this site to get full details of pointers in C.
http://www.cs.cf.ac.uk/Dave/C/node10.htm...
Good bye.
Reply:What a silly qn?
operators in java
Questions related to c %26amp; java?
lol
Reply:Yes, There is a concept in C called "Pointers" with which you can have variables that contain Address as their value.
And yes, There is a concept in every programming language, which is called "Operators", that refers to those special symbols used to perform a simple task. For example, the "++" Operator (without the quotes) is used to increase the amount of an integer value by 1.
What else do you want to know about them?
Reply:Go to this site to get full details of pointers in C.
http://www.cs.cf.ac.uk/Dave/C/node10.htm...
Good bye.
Reply:What a silly qn?
Is it true that pointers in C increase execution speed of the program and why?
I heard 2 arguments.First is that as pointers directly work with memory execution speed increases.Second is that ,memory handling is difficult ,so execution speed decreases.
Is it true that pointers in C increase execution speed of the program and why?
it can be true, depending on how you use them. For example if you pass a pointer to a class or function, you have ONLY passed that memory address to that class/function (and not copied the value in the variable which contains the data) which means that you have passed that variable much faster than if you were to use an actual non pointer variable or pass a variable by value.
Pointers are fast because they contain only memory addresses so the entire contents of the variable is not copied and passed which would be much slower.
Also to answer the second part of your question, yes in large programs memory handling can be difficult, pointers are active for the scope they are placed in (provided the programmer is linking to an object ONLY, and put a deconstructor into that object), put one at the start of your program and forget to delete it, well then now you have a major problem (a memory leak). I had a programmer friend that complained about his boss always saying, son its not your memory you need to stop pretending like it is, he was fresh out of college when he got that job, and his programming skill did increase when he got it.
Honestly though, it really does depend on the way you use them as well as the algorithm you go by.
Reply:pointers unlike variables do not store the value. instead they store the memory address of the variable that stores value. when we want to get the value pointed by the pointer using *ptr then the system do not need to find the memory of the value since it is already stored by the pointer. this makes working fast. u said that execution speed increases which is true.
Also, u said that memory handling is difficult. either u have misunderstood the question is a problem. one of the solution to ur next question is that pointer deal with memory directly. so it is very difficult to debug( find error ) from a program using pointers. so this makes the program development slow and not program efficiency slow. this is other concept beyond programming and is studied in software engineering.
i think u have under stood the use of pointer. it has merit of fast execution but demerit of slow program development.
Reply:Yes, pointers increase the execution speed because it directly points to the location of a variable, the value can be retrieved and processed in a short span of time.
Reply:Pointers don't magically increase or decrease anything. In some instances, pointers can be detrimental. So, what is a pointer? It's a variable that contains an address to a memory location. This is important: pointers are still variables. They take memory as well. If you're pointing to a char byte, using a pointer instead of a char isn't going to increase your efficiency, and may decrease it instead.
Pointers are useful when passing variables around in say, functions. What happens in C is that the variable is copied. So let's say you had a 100KB variable (like an array) in a struct. You don't want to pass that struct around by value, because your program would be forced to copy those 100KB. It's more efficient to refer to that memory location with a pointer, and then use that pointer to refer back to the value.
Note that you can shoot yourself in the foot with pointers. What happens if you have a memory location only known by its pointer, and then forget about the pointer (your pointer var goes out of scope, you accidentally assign another memory address, etc.)? You don't have a way to access the original memory location, and that memory sits around unused. Known as memory leaks.
Reply:Pointers increases the execution speed ,its rite...
it is one of the advantages of pointers,thts y we r going for pointers!!
Is it true that pointers in C increase execution speed of the program and why?
it can be true, depending on how you use them. For example if you pass a pointer to a class or function, you have ONLY passed that memory address to that class/function (and not copied the value in the variable which contains the data) which means that you have passed that variable much faster than if you were to use an actual non pointer variable or pass a variable by value.
Pointers are fast because they contain only memory addresses so the entire contents of the variable is not copied and passed which would be much slower.
Also to answer the second part of your question, yes in large programs memory handling can be difficult, pointers are active for the scope they are placed in (provided the programmer is linking to an object ONLY, and put a deconstructor into that object), put one at the start of your program and forget to delete it, well then now you have a major problem (a memory leak). I had a programmer friend that complained about his boss always saying, son its not your memory you need to stop pretending like it is, he was fresh out of college when he got that job, and his programming skill did increase when he got it.
Honestly though, it really does depend on the way you use them as well as the algorithm you go by.
Reply:pointers unlike variables do not store the value. instead they store the memory address of the variable that stores value. when we want to get the value pointed by the pointer using *ptr then the system do not need to find the memory of the value since it is already stored by the pointer. this makes working fast. u said that execution speed increases which is true.
Also, u said that memory handling is difficult. either u have misunderstood the question is a problem. one of the solution to ur next question is that pointer deal with memory directly. so it is very difficult to debug( find error ) from a program using pointers. so this makes the program development slow and not program efficiency slow. this is other concept beyond programming and is studied in software engineering.
i think u have under stood the use of pointer. it has merit of fast execution but demerit of slow program development.
Reply:Yes, pointers increase the execution speed because it directly points to the location of a variable, the value can be retrieved and processed in a short span of time.
Reply:Pointers don't magically increase or decrease anything. In some instances, pointers can be detrimental. So, what is a pointer? It's a variable that contains an address to a memory location. This is important: pointers are still variables. They take memory as well. If you're pointing to a char byte, using a pointer instead of a char isn't going to increase your efficiency, and may decrease it instead.
Pointers are useful when passing variables around in say, functions. What happens in C is that the variable is copied. So let's say you had a 100KB variable (like an array) in a struct. You don't want to pass that struct around by value, because your program would be forced to copy those 100KB. It's more efficient to refer to that memory location with a pointer, and then use that pointer to refer back to the value.
Note that you can shoot yourself in the foot with pointers. What happens if you have a memory location only known by its pointer, and then forget about the pointer (your pointer var goes out of scope, you accidentally assign another memory address, etc.)? You don't have a way to access the original memory location, and that memory sits around unused. Known as memory leaks.
Reply:Pointers increases the execution speed ,its rite...
it is one of the advantages of pointers,thts y we r going for pointers!!
[Pointers in C] What does this mean to an array of integers?
int a[10]={1,2,3,4,5,6,7,8,9};
int *p=a;
/* what does
p++
*p++
*(p++)
(*p)++
++*p
mean? */
[Pointers in C] What does this mean to an array of integers?
Arrays are the collection of similar Data Types(i.e elements) and pointers are those variables ehich holds the physical address of another variables.
Now you code
int *p=a holds the address of first element(correct would be int *p=%26amp;a)
p++ increments the address with 2 bytes because int type acquire 2 bytes in memory
*p++ means increase value at address P to 1
++*p incremtns (i.e postincrement value at address of a)
Reply:*p=a p will have the address of the variable a
p++ is post increment if we have a address as 1000 then the address will be incremented by 1 by post increment method.
*p++ will increment the value of the data present in the p
*(p++) first the value of p will be increment then address
(*p)++ this will increment the address of p value
++*p this will preincrement the p value
/* */ this is treated as multi line comment
int *p=a;
/* what does
p++
*p++
*(p++)
(*p)++
++*p
mean? */
[Pointers in C] What does this mean to an array of integers?
Arrays are the collection of similar Data Types(i.e elements) and pointers are those variables ehich holds the physical address of another variables.
Now you code
int *p=a holds the address of first element(correct would be int *p=%26amp;a)
p++ increments the address with 2 bytes because int type acquire 2 bytes in memory
*p++ means increase value at address P to 1
++*p incremtns (i.e postincrement value at address of a)
Reply:*p=a p will have the address of the variable a
p++ is post increment if we have a address as 1000 then the address will be incremented by 1 by post increment method.
*p++ will increment the value of the data present in the p
*(p++) first the value of p will be increment then address
(*p)++ this will increment the address of p value
++*p this will preincrement the p value
/* */ this is treated as multi line comment
Whenever I use pointers in C++ ,the whole compiler closes leaving an error dialog box saying some 32Bitsystem
It happens only with pointers and not character arrays.
It says the NVRAM has encountered a serious error and needs to close.
Whenever I use pointers in C++ ,the whole compiler closes leaving an error dialog box saying some 32Bitsystem
make sure you delete space that you are not using, with delete[];you are not allocating/deallocation memory right, dynamic memory is pretty tough to understand. Your not going to get it for a while. Read about it for a lil bit and get the hang of it.
survey monkey
It says the NVRAM has encountered a serious error and needs to close.
Whenever I use pointers in C++ ,the whole compiler closes leaving an error dialog box saying some 32Bitsystem
make sure you delete space that you are not using, with delete[];you are not allocating/deallocation memory right, dynamic memory is pretty tough to understand. Your not going to get it for a while. Read about it for a lil bit and get the hang of it.
survey monkey
Help with Pointers in C?
A program to read in an array of names and to sort them in alphabetical order .And use a sort function that receives pointers to the functions strcmp and swap.sort in turn should call these functions via pointers
Help with Pointers in C?
So, what's your question?
Help with Pointers in C?
So, what's your question?
What is diffrence between Function pointer & pointer function?
Question Related to pointers in c.
What is diffrence between Function pointer %26amp; pointer function?
May be, if you clearify the question more ...i would have been able to help ya.....because as far as i know, there are no such terms in C....there exist terms in C like "pointer to function" and "functions with arguments as pointers" .....so clearify it first.....
Reply:difference bet function pointer and pointer function.
lets say "fp" as function pointer and "pf" as pointer function
"fp" is a pointer- it is pointing to some function say f(x)
"pf" is not a pointer- rather it is a function which describes the nature of a pointer. now why should we call this function as "pf"? why cant we just call it as "function" ??-bcoz this function will ultimately act as a pointer.
What is diffrence between Function pointer %26amp; pointer function?
May be, if you clearify the question more ...i would have been able to help ya.....because as far as i know, there are no such terms in C....there exist terms in C like "pointer to function" and "functions with arguments as pointers" .....so clearify it first.....
Reply:difference bet function pointer and pointer function.
lets say "fp" as function pointer and "pf" as pointer function
"fp" is a pointer- it is pointing to some function say f(x)
"pf" is not a pointer- rather it is a function which describes the nature of a pointer. now why should we call this function as "pf"? why cant we just call it as "function" ??-bcoz this function will ultimately act as a pointer.
Sorting 5 numbers using Pointers and Arrays in C++?
Hi i need to write code to sort out 5 numbers using pointers and arrays in C++. I take 5 numbers from user input. I am using a 1 dimensional array but i am not sure how i can sort them out. I have written some code below but its not sorting them out.
i need to show the unsorted sequence and then show the sorted sequence in 3 passes.
I havent much used pointers so can someone help me out on this please?
Here is my code
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int numbers [5];
int i;
int *ptr;
int *ptr=numbers;
cout%26lt;%26lt;"Please enter 5 numbers to be sorted:\n\n";
for (i = 0; i %26lt; 5 ; i++ )
cin %26gt;%26gt; numbers[i];
cout%26lt;%26lt;"\nUnsorted:\n";
for (i = 0; i %26lt;= 5-1; i++)
{
cout%26lt;%26lt; numbers [i]%26lt;%26lt;" ";
cout%26lt;%26lt;"\n\nSorted:\n";
}
for (i = 0; i %26lt;= 5-1; i++)
cout%26lt;%26lt;numbers[i]%26lt;%26lt;" ";
return 0;
}
Sorting 5 numbers using Pointers and Arrays in C++?
since you said it is not sorting,no wonder!
you need to use
the statement using
s=a[0];
for(i=0;i%26lt;4;i++)
{
if(s%26lt;a[i])
{
s=a[i];
}
}
i need to show the unsorted sequence and then show the sorted sequence in 3 passes.
I havent much used pointers so can someone help me out on this please?
Here is my code
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int numbers [5];
int i;
int *ptr;
int *ptr=numbers;
cout%26lt;%26lt;"Please enter 5 numbers to be sorted:\n\n";
for (i = 0; i %26lt; 5 ; i++ )
cin %26gt;%26gt; numbers[i];
cout%26lt;%26lt;"\nUnsorted:\n";
for (i = 0; i %26lt;= 5-1; i++)
{
cout%26lt;%26lt; numbers [i]%26lt;%26lt;" ";
cout%26lt;%26lt;"\n\nSorted:\n";
}
for (i = 0; i %26lt;= 5-1; i++)
cout%26lt;%26lt;numbers[i]%26lt;%26lt;" ";
return 0;
}
Sorting 5 numbers using Pointers and Arrays in C++?
since you said it is not sorting,no wonder!
you need to use
the statement using
s=a[0];
for(i=0;i%26lt;4;i++)
{
if(s%26lt;a[i])
{
s=a[i];
}
}
I cant understand pointers and structures in C programming?
i know the basics but can anyone tell me tricks and basic things to remember for sure. i get confused when it comes to pointers and arrays and there connection and when to use *p and when to use %26amp;p and when not to. can anyone summarize the rules and common mistakes with pointers and structures in C programming. Also string literal basics as well. Thanks.
Whateveryou can explain will definitely help!
I cant understand pointers and structures in C programming?
Homestar is right as far as he goes. He uses a type called "string" which wasn't part of the ansi c language last Og checked - and quite frankly, strings, char arrays, are almost everybody's first introduction to the finer points of pointers vs the object to which the pointer refers. Og like to suggest picking up a copy of K%26amp;R (because it's thin and pretty easy to understand) and maybe suggest you throw references into the mix (references keep you from making 'stupid' mistakes with pointers - things like freeing memory that you shouldn't).
Everybody write this once:
char *myname = "Og";
char[] firstName = "Og";
char[2] alsoFirstName;
strncpy( alsoFirstName, myname, strlen(myname) );
printf( "myname: %s\n", myname ); // s.b. ok
printf( "firstName: %s\n", firstName ); // also ok
printf( "alsoFirstName: %s\n", alsoFirstName ); // oops!
Why does that third one fail? Because the array doesn't actually have space allocated for two characters plus one terminator (\0) symbol.
Note the different forms of initializing a string pointer, as well (all three variables can act like a char const*). In the first case the string (the two letters 'O', 'g' and the terminator) are kept in some data segment that never changes. In the second and third examples, the string is stored on the stack.
Anyway, this is pretty deep topic. Maybe e-mail work better. Barring any real concrete examples just remember: const and %26amp; (reference type) are your friends.
Reply:Structures are a collection variables under a name.Each variable within the structure can be accessed using the '.' mark.
An example would be this
struct profile{
string name;
int age;
}person1;
As you can see i have created a new structure called Profile and a new instance of this structure called person1.Now if i want to store anything about person1 i would do it like this.
person1.name = "Joe Schmoe"
As you can see i use the '.' mark to access the variables within the structure.Hope that helps you.
As for arrays,they are also a collection of variables,however they all have one datatype.To declare an array i do so.
int age[2]
The number within the square brackets indicates i want two integers named age.
To access each part of this array you also use the square brackets.However you must start counting from 0 when you want to access an array.Below is an example of me initializing both these integers.
age[0] = 65;
age[1] = 23;
I hope you understood me clearly ^_^.
Im not very good with pointers so bare with me.
A pointer points to a space in memory,a pointer will store this place in memory as an address.Declaring a pointer is as easy as declaring any other datatype,but you must place an asterisk (*) after the datatype.
e.g int* my_pointer;
Now to use this pointer we must assign it the address of another variable.For example say we had a variable called "age".To point to this variable in memory we must precede its name with the Ampersand sign (%26amp;).
e.g my_pointer = %26amp;age ;
Now if we were to output this pointer,it would give us the memory address of the variable age.
EDIT:Sorry i code in c++ i forgot to not use strings,however you should understand how structures work :)
Reply:OK... three questions in one.
Firstly pointers. Pointers aren't variables - but they do point to them. Pointers are defined using a * between the type and the variable, for example:
int *pointer
or
int* pointer
The latter makes more sense, as you're defining a pointer to an integer, and not an integer. However, it can cause confusion if you define normal integers on the same line, for example:
int *pointer,variable
or
int* pointer,variable
both define pointer as a pointer to an integer, and variable to an integer.
Pointers let you have multiple instances of the same variable. For example:
int i=3; /* initialise an integer */
int *p; /* create an integer pointer */
p=%26amp;i; /* and point it to the address of i */
*p=5; /* set value of integer pointed to by p, to 5
printf("%d %d",i,*p); /* print out value of both variables */
Not too useful, since I could have just used i and not the pointer? True. However, this comes into its own with allocated blocks of memory and (more simply) arrays.
See http://www.cprogramming.com/tutorial/c/l... for more details.
Next, structures. Basically, a group of variables tied together. For example, you could have an address-book with a structure 'addressBookEntry' for each entry, and within the structure you could have a name, address, phone-number, etc - but which could be referenced by a single variable name (or array) so that all the associated data is kept together within the same structure. Like a box.
I'll point you at the same site, next chapter for more info.
http://www.cprogramming.com/tutorial/c/l...
Finally, string literals. These are defined within source-code as the value of a quoted string. For example:
"This is a string literal"
Again, more info at the same site:
http://www.cprogramming.com/tutorial/c/l...
online survey
Whateveryou can explain will definitely help!
I cant understand pointers and structures in C programming?
Homestar is right as far as he goes. He uses a type called "string" which wasn't part of the ansi c language last Og checked - and quite frankly, strings, char arrays, are almost everybody's first introduction to the finer points of pointers vs the object to which the pointer refers. Og like to suggest picking up a copy of K%26amp;R (because it's thin and pretty easy to understand) and maybe suggest you throw references into the mix (references keep you from making 'stupid' mistakes with pointers - things like freeing memory that you shouldn't).
Everybody write this once:
char *myname = "Og";
char[] firstName = "Og";
char[2] alsoFirstName;
strncpy( alsoFirstName, myname, strlen(myname) );
printf( "myname: %s\n", myname ); // s.b. ok
printf( "firstName: %s\n", firstName ); // also ok
printf( "alsoFirstName: %s\n", alsoFirstName ); // oops!
Why does that third one fail? Because the array doesn't actually have space allocated for two characters plus one terminator (\0) symbol.
Note the different forms of initializing a string pointer, as well (all three variables can act like a char const*). In the first case the string (the two letters 'O', 'g' and the terminator) are kept in some data segment that never changes. In the second and third examples, the string is stored on the stack.
Anyway, this is pretty deep topic. Maybe e-mail work better. Barring any real concrete examples just remember: const and %26amp; (reference type) are your friends.
Reply:Structures are a collection variables under a name.Each variable within the structure can be accessed using the '.' mark.
An example would be this
struct profile{
string name;
int age;
}person1;
As you can see i have created a new structure called Profile and a new instance of this structure called person1.Now if i want to store anything about person1 i would do it like this.
person1.name = "Joe Schmoe"
As you can see i use the '.' mark to access the variables within the structure.Hope that helps you.
As for arrays,they are also a collection of variables,however they all have one datatype.To declare an array i do so.
int age[2]
The number within the square brackets indicates i want two integers named age.
To access each part of this array you also use the square brackets.However you must start counting from 0 when you want to access an array.Below is an example of me initializing both these integers.
age[0] = 65;
age[1] = 23;
I hope you understood me clearly ^_^.
Im not very good with pointers so bare with me.
A pointer points to a space in memory,a pointer will store this place in memory as an address.Declaring a pointer is as easy as declaring any other datatype,but you must place an asterisk (*) after the datatype.
e.g int* my_pointer;
Now to use this pointer we must assign it the address of another variable.For example say we had a variable called "age".To point to this variable in memory we must precede its name with the Ampersand sign (%26amp;).
e.g my_pointer = %26amp;age ;
Now if we were to output this pointer,it would give us the memory address of the variable age.
EDIT:Sorry i code in c++ i forgot to not use strings,however you should understand how structures work :)
Reply:OK... three questions in one.
Firstly pointers. Pointers aren't variables - but they do point to them. Pointers are defined using a * between the type and the variable, for example:
int *pointer
or
int* pointer
The latter makes more sense, as you're defining a pointer to an integer, and not an integer. However, it can cause confusion if you define normal integers on the same line, for example:
int *pointer,variable
or
int* pointer,variable
both define pointer as a pointer to an integer, and variable to an integer.
Pointers let you have multiple instances of the same variable. For example:
int i=3; /* initialise an integer */
int *p; /* create an integer pointer */
p=%26amp;i; /* and point it to the address of i */
*p=5; /* set value of integer pointed to by p, to 5
printf("%d %d",i,*p); /* print out value of both variables */
Not too useful, since I could have just used i and not the pointer? True. However, this comes into its own with allocated blocks of memory and (more simply) arrays.
See http://www.cprogramming.com/tutorial/c/l... for more details.
Next, structures. Basically, a group of variables tied together. For example, you could have an address-book with a structure 'addressBookEntry' for each entry, and within the structure you could have a name, address, phone-number, etc - but which could be referenced by a single variable name (or array) so that all the associated data is kept together within the same structure. Like a box.
I'll point you at the same site, next chapter for more info.
http://www.cprogramming.com/tutorial/c/l...
Finally, string literals. These are defined within source-code as the value of a quoted string. For example:
"This is a string literal"
Again, more info at the same site:
http://www.cprogramming.com/tutorial/c/l...
online survey
Polymorphism in C++?
Does C++ require that pointers be used to use Polymorphism in C++?
Why I ask:
I created a simple class Vehicle which has nothing except a member function called "showType()" which displays "I am a vehicle". I then created a subclass of Vehicle, Car, with a function called "showType()" which displays "I am a car". The function showType() is marked as virtual in the base class.
If I write the following code:
Vehicle test[10] ;
Car c ;
test[0] = c ;
test[0].showType() ;
The output "I am a Vehicle is produced" -- clearly no polymorphism.
However, if I change to using pointers:
Vehicle* test[10] ;
Car c ;
test[0] = %26amp;c ;
test[0]-%26gt;showType() ;
This shows the result I expect -- "I am a car". Does C++ only support Polymorphism with pointers? In some sense this seems to be logical since Polymorphism is a runtime feature, however I was a little disappointed that it couldn't distinguish in the non-pointer case -- I was hoping for a container type effect similar to Java.
Polymorphism in C++?
C++ always uses pointers to work with polymorphism and other OOP subjects.
Why I ask:
I created a simple class Vehicle which has nothing except a member function called "showType()" which displays "I am a vehicle". I then created a subclass of Vehicle, Car, with a function called "showType()" which displays "I am a car". The function showType() is marked as virtual in the base class.
If I write the following code:
Vehicle test[10] ;
Car c ;
test[0] = c ;
test[0].showType() ;
The output "I am a Vehicle is produced" -- clearly no polymorphism.
However, if I change to using pointers:
Vehicle* test[10] ;
Car c ;
test[0] = %26amp;c ;
test[0]-%26gt;showType() ;
This shows the result I expect -- "I am a car". Does C++ only support Polymorphism with pointers? In some sense this seems to be logical since Polymorphism is a runtime feature, however I was a little disappointed that it couldn't distinguish in the non-pointer case -- I was hoping for a container type effect similar to Java.
Polymorphism in C++?
C++ always uses pointers to work with polymorphism and other OOP subjects.
Subscribe to:
Posts (Atom)