Tuesday, July 14, 2009

How can I use pointers in C# programming?

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


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 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

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.


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


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.


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

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 ;)


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...


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


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

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?


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!!


[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


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

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?


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.


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 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

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.


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++?
The difference, Sharpie, between


test[0] = c ;


and


test[0] = %26amp;c ;


is that in the first case you are copying an instance of something to a Vehicle. In that event, all of c that is a Vehicle will be copied to test[0], and test[0] will have none of the Carness. C will remain a Car and test[0] will remain only a Vehicle.


However, %26amp;c is a pointer, so regardless of type it is cast to, it will always be a pointer to an instance of a Car, even if it is cast to a Vehicle pointer.


As a rule of thumb, yes, you will need to deal only with pointers if you are going to make polymorphism work. However, according to the technical definition, a reference is not a pointer, but as far as the machine code is concerned, it is, so this


Vehicle %26amp;v = c;


v.showType();


would also reveal it to be a Car.
Reply:That is the correct behavior in both cases.





You will only see polymorphic behavior with pointers and references.





My java is pretty rusty but I don't recall that it is different in this respect. I suspect you were storing references (whether or not you realized it) in the containers.
Reply:Its supported!!





I also am studying programming and do not know but here:


http://www.google.com/search?hl=en%26amp;q=pol...


Im having trying to understand the concept of pointers in c++?

I understand that they point to the location of the integer but why would u use a pointer. It makes no sense to me. Many program use them but why, Thnx

Im having trying to understand the concept of pointers in c++?
Pointers are used alot in data structures (as well as many other things).





For example, you can have what is known as a linked list. This is a list that contains items that link to the next item in the list via pointer. This allows you alot of flexibilty and speed in "moving around the data" logically because you simply have to change the pointer, not shift all the data around in memory.





This is an example of a few items in a linked list:





Name: NodeA


Data: A


Next: Address of NodeB





Name: NodeB


Data: B


Next: Address of NodeC








Name: NodeC


Data: C


Next: Address of NodeD





etc.





Now if you wanted to delete node b, you would just have to change the next on node a to node c, rather than having to delete the data for node b and then move all items after it down a section to take its place.
Reply:Pointers are used for several reasons.





Normally you would use a pointer to point to an object. That object may have different characteristics based on something in the record. In one instance it may point to car object and later a bus object. It can be easier to handle using pointers.





Pointers can also be used point to an parameter to a function call. This allows the function to operate on the real object rather than a copy. Passing copies back and forth can be wasteful in both time and space.





Text me if I haven't helped.


Need help with pointers in C?

How would I write this and why isn't it working?





I have a pointer pointing to the first character in a string. I want to store this string to the first entry in a two-dimensional array called "paths". Why isn't my loop storing the string?





char* token;


while (token != NULL)


{


paths[ 0 ][ j ] = %26amp;token;


token++;


j++;


}

Need help with pointers in C?
You didn't assign token to anything. Also, you appear to be confusing address %26amp; with object pointed to *.





char szMystring[] = "I have a problem.";


char* token;


char paths[2][64];





token = %26amp;szMystring[0];


int j = 0;


while (*token != 0)


{


paths[ 0 ][ j ] = *token;


token++;


j++;


}

salary survey

About pointers in c with example?

i need a daily e mail from u about programming in c with example

About pointers in c with example?
Yeah - right! I'm going to e-mail you DAILY with examples?





Try using the Yahoo search engine to find them.





As a search, try this:


http://search.yahoo.com/search?p=%22C%22...


Incrementing array of pointers in C?

Can someone explain to me why postfix and prefix increment of pointer to pointer works differently, please?





My code is as follow:





#include %26lt;stdio.h%26gt;





int main()


{


char* s[ ] = {"hello", "how", "are", "you"};


char ** p;





p= s;


printf("%s ", *p); //print out hello expected


*p++;


printf("%s", *p); //print out how as expected


++*p;


printf("%s", *p); //print out "ow". expecting "are"





}





My question is if I *p++, p points to next string in the array. But when I do *p++, it points to the next word of the string it is pointing to. Why??





Thank you very much in advance.

Incrementing array of pointers in C?
The problem does not lie in preincrement or postincrement


The problem lies in operator precedence.





p=s =%26gt; *p = s[0]


Now *p++ resolves to *(p++) = s[1]


Then ++*p resolves to ++(*p) = s[1][1]


What I think you needed was *(++p) = s[2]





Its always advised to use brackets to ensure that you clearly understand what precedence you are expecting.





Hope this helps.


Pointers in C++?

For instance, in C++:





int *x;


int *y = new int;


*y=16;


x=y;


delete y;


cout%26lt;%26lt;x%26lt;%26lt;endl;





what will be printed out (nothing or 16)? and why? thanks a lot

Pointers in C++?
Asuming that the cout line is cout %26lt;%26lt; *x %26lt;%26lt; endl:


======================================


The right answer to this question is: undefined. If the particluar memory space still contains 16, it may print 16. But if any other program has now snacthed it and used it for some other purpose, it may be that value. It may be garbage, it may crash, it may punch you on the nose with a hand coming out of the monitor.





The right advice for people who try such things: use C++ the right way. Respect the flexibility that pointers allow you to have. Do not try to access memory that you have not been assigned or you have deleted. What you are trying to do is interacting with a dead person. The ghost might get you!





And yes, I forgot to mention that some compilers do set the value to zero, as one of the answers above states it will be zero. That answer is strictly compiler specific. And such a compiler is too gentlemanly I would say. You should try to write code in such a way that most if not all good compilers run it smoothly.
Reply:0 is printed due to null pointer assignment
Reply:The answer is 0





If you comment out the line


delete y;





then the answer is 16.
Reply:The correct answer is it's undefined, because you're accessing memory that's been deallocated.
Reply:It will print out the memory location of y because you assigned the y to int x, but y is empty.
Reply:it wont print anything,


the results will pretty much just be nothing,


you should download dev-c++
Reply:I compiled your code and ran it and I got an outcome of: 0x32460





I'm not sure why this is because I'm new to C++ (and programming in general).


Pointers in C language?

What are two ways i could assign the address of an array element [3][2] to a pointer?

Pointers in C language?
like this





int *p;


p = %26amp;array[3][2];
Reply:int *ptr;


ptr = %26amp;array[3][2];





and





int *ptr1,*ptr2;


ptr1 = %26amp;array[3][2];


ptr2 = ptr1;
Reply:int* p;


p = %26amp;a[3][2];


int* q;


q = a[3] + 2;





In the first one, the "%26amp;" just means "the address of".





In the second one, because it's a 2-dimensional array a[3] just gives me the address of the beginning of the 4th row (row 1 would be a[0]), which I just add 2 to in order to advance the address 2 places further down the row.

surveys

What is wild pointer , Null Pointer,Void pointer & Dangling Pointer? How can we distinguish between them?

Related To Pointer in c

What is wild pointer , Null Pointer,Void pointer %26amp; Dangling Pointer? How can we distinguish between them?
In computer science, a pointer is a programming language datatype whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer. A pointer is a simple implementation of the general reference datatype (although it is quite different from the facility referred to as a reference in C++).





Null Pointer


A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. Null pointers are used routinely, particularly in C and C++, to represent exceptional conditions such as the lack of a successor to the last element of a linked list, while maintaining a consistent structure for the list nodes. This use of null pointers can be compared to the use of null values in relational databases and to the "Nothing" value in the "Maybe" monad. In C, each pointer type has its own null value, and sometimes they have different representations.





Because it refers to nothing, an attempt to dereference a null pointer can cause a run-time error that usually terminates the program immediately (in the case of C, often with a segmentation fault, since the address literally corresponding to the null pointer will likely not be allocated to the running program). In Java, access to a null reference triggers a Java.lang.NullPointerException, which can be caught (but a common practice is to attempt to ensure such exceptions never occur). In safe languages a possibly null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case; in fact, a possibly-null pointer can be seen as a tagged union with a computed tag.





A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to a pointer to any object or function, whereas an uninitialized pointer might have any value. Two separate null pointers are guaranteed to compare equal.





Void Pointer


a void pointer (void *), which indicates that it is a pointer to a region of unknown data type. This pointer is typically cast to a more specific pointer type by the programmer before being used.


malloc returns a void pointer





Dangling Pointer


A reference that doesn't actually lead anywhere. In C and some other languages, a pointer that doesn't actually point at anything valid. Usually this happens because it formerly pointed to something that has moved or disappeared, e.g. a heap-allocated block which has been freed and reused.





Hope this will satisfy you.





R. Nag MS (Comp. Sc)


How pointers make a C program faster?

can use of pointers make a C program faster?

How pointers make a C program faster?
They allow direct memory manipulation.
Reply:Yes, using pointers will help you to control computer memory





For tutorials click here.





http://www.dreamincode.net/forums/showto...


What u mean by pointers(c-prgraming)?

Pointers literally mean what they say, they point to a memory location. Say for instance you have stored a variable x in a memory location. y is a pointer to the location of x. All it says is that I point to x and x is at this location and has a certain value. Pointer programming is very useful and is extensively used for several complicated problems. Pointers have a constant size of 4 bytes and can be used to point to variables of any type: char, int, long, etc.

What u mean by pointers(c-prgraming)?
They are special variables which hold memory address of another variable


a pointer is a separate variable that refers to another location in memory


C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?

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

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


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;


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 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

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 !!


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................


How can pointers in C++ be usefull in C++?

i'm reading about pointers in C++ and they dont really seam to be worthwhile or am i missing something?

How can pointers in C++ be usefull in C++?
Pointers provide a lot of power within a C++ (or C) application. You can allocate memory in one spot and have it persist for as long as you need it, as long as you delete them memory allocation before the application exits. This persistence of memory provides for a lot of flexibility because the memory can be accessed anywhere from inside the application as long as you provide the pointer.





There is a lot you can do with pointers once you fully understand what you're dealing with. Just like the Force, though, pointers have both a light and dark side, and the dark side of pointers can lead you to crashing applications, crashing the system, or worse....
Reply:You're kidding, right?





Pointers are desirable (at times) because they allow you in, an OO architecture, to put a value or value array into memory exactly once, instead of making repeated copies of it into memory as you pass the value through your classes and methods.





But pointers move from desirable to absolutely necessary when you try and use the operating system's APIs, or the graphical subsystem's. Most API calls in, well, just about any operating system will require you to pass/retrieve pointers. You can't expect to write much more than "Hello world!" without pointers...


Structures and pointers in C#. How can I implement them?

Hi!


I want to implement the foloowing in C#:





struct mytree


{


char name[80]; // information


int noChildren; // number of children


mytree *children;


};





Is it possible? How?


Or can I do somethink like that, using C# types?


Please help!





I don't want to use pointers! (dangerous in C#)





Thanks!





PS: if you need more details, please try to imagine that


a run "tree" command from Dos Prompt (cmd.exe) and


a have to load all the folders in memory, in a structure.

Structures and pointers in C#. How can I implement them?
In C#, I would convert this to a class so that you could make use of the Generic List%26lt;T%26gt;.





class MyTreeNode


{


public string Name;


public List%26lt;MyTreeNode%26gt; MyChildren = new List%26lt;MyTreeNode%26gt;();


}





Here is an example of how its used:





class Program


{





static void Main(string[] args)


{


MyTreeNode node = new MyTreeNode();


MyTreeNode child = new MyTreeNode();


node.Name = "root";


child.Name = "child";


node.MyChildren.Add(child);


MyTreeNode childOfChild = new MyTreeNode();


childOfChild.Name = "childOfChild";


child.MyChildren.Add(childOfChild);





foreach (MyTreeNode nodeChild in node.MyChildren)


{


//access the children of root


foreach (MyTreeNode nodeGrandChild in nodeChild.MyChildren)


{


//access the children of each of root's children


}


}


Console.ReadLine();


}





This doesn't help you solve it using a C# struct, but it does get you out of using pointers.
Reply:I believe in C#, if you want to use pointers you will have to declare the struct as 'unsafe' as well. Example:





public unsafe struct mytree{ ....}





The thing about that is the fact the types, and properties declared within a piece of unsafe code will not be handled by the CLR.

salary survey

Pointers in c++ - i want only the pointers sorted?

char *cities[ ]={"New York","Washington","Durham","Cincinati"}...


char **c;


c = cities; // i want to have the pointers sorted but the cities must remain in exact place.

Pointers in c++ - i want only the pointers sorted?
Assuming you want them sorted into an order based on what they point to. Then create a parallel array in which they are placed in sorted order. Changing there order there doesn't change the location they point to or the ordering in the original array.





In the above assigning


c = cities


makes c and alias for cities. What you what to do is make a deep copy as below.








char* cities[] = {.....};


char** toSort = calloc(sizeof(cities)/sizeof(*cities), sizeof(char*));





for (int i = 0; i %26lt; sizeof(cities)/sizeof(*cities); ++i)


toSort[i] = cities[i];





//sort toSort;





cities[] is left in it's original order


the cities in toSort are ordered


I need a sample exam and answers on the dynamic data structures in C++(Pointers,Linked Lists &recursion)?

You have not stated how complex you want this exam to be. I could very easily ask questions and give answers that would not be reasonable to ask at a first year bachelors level but would be at a first year graduate level. However I will assume this is a test at a first year post secondary level.





--------------------------------------...


(1) Q: Write a funtion that prints out the contents of a singly linked list in reverse. (hint use recursion)





A:


void write_reverse(Element *first){


__if (first){


______write_reverse(first -%26gt; next );


______cout %26lt;%26lt; first -%26gt; data %26lt;%26lt; ' ';


__}


}





(2) Q: Write a funtion that uses recursion to place an element at the end of a singly linked list.





A:


void put_last_rec( Element *%26amp; first, int d){


__if (! first)


____first = new Element(0, d);


__else


____put_last_rec(first -%26gt; next , d );


}





(3) Q: Describe what the following code is:





class unknown {


public


__unknown *forward, *backward;


__int data;


__unknown(unkown *f=0, unknown *b=0, int d=0)


____ : forward(f), backward(b), data(d) {}


};





A: This is a class for a doubly linked list with values of type int. This class contains two pointers, an integer, a constructor for constructing new lists which also acts as a default constructor.





--------------------------------------...


This exam has pointers, linked lists, and recursion and should be of decent difficulty for an introduction to programming type class. Hope this helps.

survey

Can I get algorithm/source code for matrix multiplication using C pointers?

main()


{


int B[][3]={{1, 0, -2},


{0, 3, -1}};


int C[][2]={{0, 3},


{-2, -1},


{0, 4}};


int A[2][2]={{0, 0}, {0,0}};





int n1=2, n2=3, n3=2;





int i, j, k;





for( i=0; i%26lt;n1; i++)


for( k=0; k%26lt;n3; k++)


for( j=0; j%26lt;n2; j++)


(*(*(A+i)+k)) += (*(*(B+i)+j)) * (*(*(C+j)+k));





for(i=0; i%26lt;n1; i++)


{


for(j=0; j%26lt;n3; j++)


printf("%d ", A[i][j]);


printf("\n");


}


}


Can anyone tell me about online books on c++ pointers?

cout%26lt;%26lt;"Pointers are cool" lol





they r just one of the amzing feautures in C++








have a look @:


http://cplus.about.com/od/beginnerctutor...





http://www.programmingtutorials.com/cplu...





http://digg.com/programming/C_pointers,_...





feel free to pose any Questoin if u meed more help in C++





HuS

Can anyone tell me about online books on c++ pointers?
I'm sorry. I couldnt find any.


Using pointers in data structures in C?

If I create a data structure (in C) with a pointer in it, then a pointer of that structure's type:





struct data {


int a;


int *ptr;


}





struct data *info;





How do I initialize that pointer? Specifically, I'm trying to use malloc to allocate storage space for the pointer to point to.





I tried all of these:


info.*ptr = (int *)malloc(sizeof(int)*4);


*info.*ptr = (int *)malloc(sizeof(int)*4);


info.ptr = (int *)malloc(sizeof(int)*4);


*info.ptr = (int *)malloc(sizeof(int)*4);





But nothing seems to work. What am I doing wrong?

Using pointers in data structures in C?
Since you declared info as a pointer you first need to allocate the memory for that object with:





info=(struct data *)malloc(sizeof(data));





then





info-%26gt;ptr=(int*)malloc(sizeof(int)*4);





This will basically create an array in ptr that can be referenced with info-%26gt;ptr[0], info-%26gt;ptr[1], etc.





Make sure you free both sets of allocated memory when you are done with it.





Note that (*info).ptr will also work to reference that item.


How to work with c pointers?

A pointer refers to an address that points to another variable.





So say you have an integer x, and another integer y.





int x, y;





You might have a pointer to the "current" integer:





int *current;





Now you could write something like this:


current=%26amp;x; /* make current point to x */


*current=5; // set x to 5





The advantage here is that you can later set current to %26amp;y and then code that used to modify x will now modify y.





This is useful in functions where you want to modify an argument. For example:





void square(int *x)


{


*x=*x * *x;


}





You might call this as:


int z=50;


square(%26amp;z);


/* now z= 50*50 */





Of course your could write square in this case to return the result, but perhaps you are returning some result already and want to return something else. For example, suppose you want to write a function that gets two numbers and returns the largest and the smallest, you might write:





int numsort(int a, int b, int *smallest)


{


if (a%26gt;b) { *smallest=b; return a; }


else { *smallest=a; return b; } /* if equal, doesn't matter */


return 0; // not reached


}





Another important use is when working with arrays. An array name is a pointer to the first element of the array:





int x[50];





You can even have pointers to functions. For example, you might have a table of function pointers to execute different code based on an index.





Now x is the same as %26amp;x[0] and incrementing x will point at subsequent elements.





Finally, dynamic allocation uses pointers. So another way to handle the array above would be:





int *x;


x=malloc(50*sizeof(int)); /* make array of 50 integers */


/* Now x works just like an array */


. . .


free((void *)x);

How to work with c pointers?
You can also have pointers to functions. Take the addresses of various functions and put them in an arrays of function pointers.


This is useful in implementing state machines and executing different functions according to rules built around calculations rather then if..then..else constructs
Reply://Declare a pointer





int *i; // just prefix a normal variable with *





//Once you are done with your pointer just delete it


delete i;





// to access member functions in pointers use "-%26gt;" instead of "."





this-%26gt;setText("hello");





Note: This is c++, c uses the same conventions with the exception of "//" = "/* */"

land survey

Help in C pointers?

#include"stdio.h"


#include"conio.h"


void main()


{


void bubblesort(char *R[], int a); //function declaration//


char *restname[20][30]; //Variable in main//


bubblesort(restname[30], 20)//Function bubblesort in main//





void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//





}


}





help with the declaration part

Help in C pointers?
I see several potential problems...





(1) The function declaration for bubblesort goes above the main, not in it.





(2) The function itself appears within the main function. C doesn't allow nested functions.





(3) That function has as its first parameter an array of pointers to char. Fine, we sort arrays of pointers all the time. But then the sort algorithm sorts by the value of the pointer itself, not the value of the char indicated by that pointer. Sorting the pointers in this manner makes little sense.
Reply:first any function declartion and definition should be out of main scope


not inside the main,the only thing inside the main is the fucntion call :)





secondly your array restname is empty ? why ? what are you sorting ?





good luck :)





#include"stdio.h"


#include"conio.h"








void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


hold = new char;





for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//


void main()


{





char *restname[20][30]; //Variable in main//








bubblesort(restname[30], 20);//Function bubblesort in main//














}


C pointers, give practical examples?

Pointer is a variable that contains the address of another variable. (or in other words the address of a memory location). in 16-bit segmented memory model, there are two types of pointers: near pointer, that points in the same memory segment, and far pointer, which can point to beyond the current memory segment. Near pointer is actually a 16-bit integer that addresses a memory location in the segment pointed to by DS register of processor. While far pointer is 32-bit integer which contains both segment and offset address.





in 32-bit flat addressin mode, pointer is always a 32-bit integer.





One important thing to understand is pointer itself does not contain any data (or code sometimes), rather it contains the address of a memory location where data (or code) can be found. Using this technique is called indirect addressing and multiple indirection could be possible.





I give hereunder a code example which uses the pointers. Two operators in C are very important when using pointers. * is called 'value at' and %26amp; is called 'address of'. here's the code in C:





#include %26lt;conio.h%26gt;


#include %26lt;stdio.h%26gt;





void main()


{


int x=100;


int *ax=%26amp;x;





printf("%d, %d", x, *ax); // will print 100, 100


printf("%d",ax); //will print memory address of x





*ax=200; // you are actually changing x with its pointer ax





printf("%d, %d", x, *ax); // will print 200, 200


printf("%d",ax); //will print memory address of x





}





try this code in a 16-bit C version.


hope that answers the question

C pointers, give practical examples?
pointer is a variable it stores the address of another variable.





say for example


you are declaring a variable integer





int i;


i=10;


int occupies 2 bytes in the memory


say for example


memory address will look like this





8000 8001 8002 8003 ......9000





when you store it in the memory it will converted to binary and occupies the 2 bytes





here int i will occupie from 8000 to 8016





int *ptr;





ptr=%26amp;i (address of i)





so now ptr contains the starting address of i ...


here i starting address is 8000. so ptr= 8000





*ptr = value at address ( 10)


%26amp;i = address of i (8000)


%26amp;ptr= address of ptr ( prt is also variable so it will occupy some space in memmory it will return that base address





for better understanding





buy the book named "pointers in c " by yaswant kanithkar


Sunday, July 12, 2009

C++ Pointers?

Say you know a memory location address (say 0x22ff70) and you want to know what is stored there (if anything), how do you go about printing the contents on screen?

C++ Pointers?
Casting, that is the magic of C++.





Try these articles.





http://www.boost.org/libs/conversion/cas...





http://en.wikipedia.org/wiki/Pointer





http://www.informit.com/guides/content.a...





Good luck
Reply:U can store this address in a pointer. Get the value thro' *


I think its correct. I studied this long back. Sorry if am wrong
Reply:If you want to print the valud of a variable you "Printf("%i",varr). (For example)





If its a pointer you have to "Printff("%i";%26amp;point). You have to tell the printing function to print not the value of the pointer (0x22ff70 in this case) but the value of the address the pointer points :).
Reply:If you know exactly data type kept at this address, do the following:


...


known_data_type * ptr = (known_data_type) void_pointer;


// print your known data here...


...





In another case, you cah just cast this address to char* or unsigned char * and get some dump:


...


unsigned char * up = (unsigned char *) void_pointer;


dump( up, any_size_you_need_if you_know_it);


C++ pointers question?

I understand points and stuff but where would I use it? they seem useless and seem like something I would never use.





can you give me some examples and maybes some codes to help me understand when I would use them?





Thanks,


S_R_S

C++ pointers question?
Pointer code is more educational than practical. You really need to fully understand the usage of memory and pointers to store values in order to write efficient code and manage memory correctly.





My recollection from school is that most pointer operations were performed in lists, data collections, and sorting methods. These are high level ops that will already be written into the pre-existing utility libraries that you will frequently use. But you really need to understand what is going on under the covers.
Reply:We can define a variable in C++ to store a memory address. A pointer in C++ is said to "point to" the memory address that is stored in it. Also, when defining a C++ pointer variable, we must specify the type of variable to which it is pointing. For example, to define a pointer, which will store a memory address at which exists an int, we can do the following:


int *p,a;


here a is a ordinary variable,p is a pointer variable to store the address of a.


a=5;


p=%26amp;a;


where a contains the value 5 and p contains the address of a(memory address)


here '%26amp;' is a 'reference' or 'address operator'
Reply:Scott, C++ is a language of pointers. It's probably difficult for you to understand this now; however, if you keep at it, you'll eventually see the power of pointers. All the crap you're learning about classes are in fact teaching you about pointers.





Example. We have a class called location. In the location class, we have GPS Latitude and GPS Longitude and other stuff.





class location


{


DOUBLE GPSLat;


DOUBLE GPSLong;


DOUBLE Altitude;





VOID function1(....)


}





From the location class, we'll derive a Ship class and Human class.





class Ship::location


{


DOUBLE heading;


DOUBLE speed;


etc.


}





class Human::location


{


BOOL Man;


DOUBLE Height;


DOUBLE Weight;


etc.


}





From our main program we have five boats (from ship) and four Eskimos


from human.





Ship boat[5];


Human Eskimo[4];





main()


{


......





// somewhere in here we want to print the location data


// of a ship or an Eskimo. It'll be user or programmatically


// selectable





location* myTarget;


myTarget = %26amp;boat[4]; or myTarget = %26amp;Eskimo[0]; or, etc. (SOME INPUT)





printf( "%f, %f", myTarget-%26gt;GPSLat, myTarget-%26gt;GPSLong );





Note how the printf function does NOT need to change. It will


work on all ships or Eskimos and it does not include even an index


into the arrays. You would obviously make a more meaningful


routine than just a printf.





Hope this helps. (C++ in one minute).
Reply:Pointers are not easier to understand but they are much easier to understand if you go back to the C programming language. C was written originally on computers so simple you can buy calculators today which are more complicated and have more memory. For that reason, a lot of under the hood stuff was and is pretty much out in the open. An example of this would be pointers and arrays. Everything in C, we are told, is passed as a value except arrays. These are passed by the address of the first element Array[0]. In practical terms, this means when you pass an array to a function what you have is a pointer to that array, on which you can (though it's not recommended) do pointer arithmatic. You are better off creating a temporary pointer, assigning it Array[0]'s address and using that because it will help you keep track of things. With C++ you can use the %26amp; operator to pass anything by reference, and it is certainly less cumbersome, but the reason C code is supposed to compile into such fast and efficient machine code is it stays close to what the hardware is actually doing and what you are trading off is readability for efficiency. Read Kernighan and Ritchie's The C Programming Language even if you are not particularly interested in programming in C. You'll find source code for many functions in cstdio and cstlib which use pointers, and frankly which if you come to understand (and these guys are good at explaining) will help you understand pointers better than anyone at Yahoo! Answers

survey software

Why do c pointers are faster than variables accessing?

why the accesing time is fast of pointers than that of variables

Why do c pointers are faster than variables accessing?
When accessing a variable there may be a need to compute the address of the variable, while the pointer has that address already computed. For example, if the variable is an element of


an array, accessed as a[b], a computation of the address using


b would be necessary, making it slower.


C++ Pointers; Can someone use illustrations and explain?

Can someone give a simple explanation of the use of Pointers, how they work, and their purpose. Illustrations would be a +.

C++ Pointers; Can someone use illustrations and explain?
What are pointers?





Pointers are aptly named: they "point" to locations in memory. Think of a row of safety deposit boxes—of various sizes—at a local bank. Each safety deposit box will have a number associated with it, so that the teller can quickly look it up. These numbers are like the memory addresses of variables. A pointer in the world of safety deposit boxes would simply be anything that stored the number of another safety deposit box.





Perhaps you have a rich uncle who stored valuables in his safety deposit box, but decided to put the real location in another, smaller, safety deposit box that only stored a card with the number of the large box with the real jewelery. The safety deposit box with the card would be storing the location of another box; it would be equivalent to a pointer. In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables.





The cool thing is that once you can talk about the address of a variable, you'll then be able to go to that address and retrieve the data stored in it. If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data! Moreover, if you need more memory for your program, you can request more memory from the system—how do you get "back" that memory? The system tells you where it is located in memory. In other words, you get a memory address back. And you need pointers to store the memory address.





A note about terms: the word pointer can refer either to a memory address itself, or to a variable that stores a memory address. Usually, the distinction isn't really that important: if you pass a pointer variable into a function, you're passing the value stored in the pointer—the memory address. When some people want to talk about a memory address, they refer to it as a memory address. When they want a variable that stores a memory address, they call it a pointer. When a variable stores the address of another variable, they say that it is "pointing to" that variable.


___________





Pointer Syntax


___________





Pointers require a bit of new syntax because when you have a pointer, you need the ability to request both the memory location it stores and the value stored at that memory location. Moreover, since pointers are somewhat special, you need to tell the compiler when you declare your pointer variable that the variable is a pointer, and tell the compiler what type of memory it points to.





The pointer declaration looks like this:





%26lt;variable_type%26gt; *%26lt;name%26gt;;





For example, you could declare a pointer that stores the address of an integer with the following syntax:





int *pointer1; // points to an integer





Notice the use of the *. This is the key to declaring a pointer; if you add it directly before the variable name, it will declare the variable to be a pointer.





Important: if you declare multiple pointers on the same line, you must precede each of them with an asterisk:





// one pointer, one regular int


int *pointer1, non_pointer1;





// two pointers


int *pointer1, *pointer2;





As I mentioned, there are two ways to use the pointer to access information: it is possible to have it give the actual address to another variable. To do so, simply use the name of the pointer without the *. However, to access the actual memory location, use the *. The technical name for this doing this is 'dereferencing the pointer.' Basically, you're taking the reference to some memory address and following it, to retrieve the actual value. It can be tricky to keep track of when you should add the asterisk. Remember that the pointer's natural use is to store a memory address; so when you use the pointer, i.e., pointer1, then, it evaluates to the address. You have to add something extra—the asterisk (*pointer1)—in order to retrieve the value stored at the address. You'll probably do that an awful lot. Nevertheless, the pointer itself is supposed to store an address, so when you use the bare pointer, you get that address back.





Pointing to Something: Retrieving an Address





In order to have a pointer actually point to another variable it is necessary to have the memory address of that variable also. To get the memory address of a variable (its location in memory), put the %26amp; sign in front of the variable name. This makes it give its address. This is called the address-of operator, because it returns the memory address.





Conveniently, both ampersand and address-of start with 'a;' that's a useful way to remember that you use %26amp; to get the address of a variable.





For example:





#include %26lt;iostream%26gt;





using namespace std;





int main()


{


int x; // A normal integer


int *p; // A pointer to an integer





p = %26amp;x; // Read it, "assign the address of x to p"


cin%26gt;%26gt; x; // Put a value in x, we could also use *p here


cin.ignore();


cout%26lt;%26lt; *p %26lt;%26lt;"\n"; // Note the use of the * to get the value


cin.get();


}





The cout outputs the value stored in x. Why is that? Well, let's look at the code. The integer is called x. A pointer to an integer is then defined as p. Then it stores the memory location of x in pointer by using the address-of operator (%26amp;) to get the address of the variable. Using the ampersand is a bit like looking at the label on the safety deposit box to see its number rather than looking inside the box, to get what it stores. The user then inputs a number that is stored in the variable x; remember, this is the same location that is pointed to by p.





The next line then passes *p into cout. *p performs the "dereferencing" operation on p; it looks at the address stored in p, and goes to that address and returns the value. This is similar to looking inside a safety deposit box only to find the number of (and, presumably, the key to ) another box, which you then open.





Notice that in the above example, pointer is initialized to point to a specific memory address—before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the program. For instance, the operating system will probably prevent you from accessing memory that it knows your program doesn't own: or else, your program would crash. If it let you use unowned memory, you could mess with the memory of any running program. For example, if you had a document opened in Word, you could change the text! Fortunately, Windows and other modern operating systems will stop you from accessing that memory—by causing your program to crash. To avoid crashing your program, you should always initialize pointers before you use them.





[Side note: It is also possible to initialize pointers using free memory. This allows dynamic allocation of array memory. It is most useful for setting up structures called linked lists.]





The keyword 'new' is used to initialize pointers with memory from free store (a section of memory available to all programs). The syntax looks like the example:





int *ptr = new int;





It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage.





The delete operator frees up the memory allocated through new. To do so, the syntax is as in the example:





delete ptr;





After deleting a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer, the pointer becomes a null pointer, in other words, it points to nothing. By doing this, when you do something foolish with the pointer, you find out immediately—instead of later, when you have done considerable damage.





In fact, the concept of the null pointer is frequently used as a way of indicating a problem—for instance, some functions left over from C return 0 if they cannot correctly allocate memory (notably, the malloc function).