Tuesday, July 14, 2009

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.