Sunday, July 12, 2009

A program in C++ pointers.?

please help me write a program to search for a given character and print the streing from the point of match using pointers.





i know to do it without pointers but i am asked by my sir to do it using pointers. please help me. you wouldn't be doing my home work but really helping me......


please help

A program in C++ pointers.?
First things first, 'char * s' is the same as the character array 'char s[].' In this example, s is what is commonly called a c-style string, which is a null-terminated array of characters.





For example, if you use the string 'cat', the c-string would be the character array {'c','a','t',0 (the number zero, not the character zero)}.





Just to note, c-strings can be initialized easily like this:


char s[] = "cat";





As I said before, char * s is the same as char s[] and they are interchangeable. In both cases, s points to the address of the first element of the array. When you say s[index], you are getting the value of the object at memory location s + index. You can do the same thing with pointers by first adding index to s and then dereferencing s, like so: *(s+index). This is what is going on in this example.





Lets say we're using the string 'cat' and the character we're looking for is 'a'. So this algorithm starts off with 'cat' being stored in a null-terminated character array s.





%26gt;while((c!=*s) %26amp;%26amp; (*s)){





This will loop as long as the character at s in not equal to character c and while the character at s in not equal to zero. For our first iteration, s is still pointing to the first character of the string and by dereferencing s (via *s) we get the character 'c'. Since this is neither equal to the character stored in c, 'a', nor is it equal to zero, we will continue.





When the end of the string is reached and s points to the null-terminating character, zero, the while loop will end because in C a value of zero is the same as false.





%26gt;s++; }





This increments the pointer, essentially behaving as an iterator. When you increment a pointer, it then points to the next character in the array, which in our case would be 'a'. Also note that the string represented by c-strings go from the character the pointer points to the null-character, so doing this effectively removes the first character and makes the string 'at'.





After s is incremented, the next iteration will exit the while loop because now *s = 'a'





%26gt;return s;





this obviously returns s, which now points to the c-string 'at'.





To hide the confusing parts of pointers, you could also do it like this:





int index = 0;


while( s[index] != c %26amp;%26amp; s[index] ) {


index++;


}


return s;





This method still uses pointers, except they are referenced in a more easily understood way.





Pointers are a very important topic. They may seem difficult or confusing at first, but once you get the hang of them they can be a very powerful and useful tool that will make your life much much easier.
Reply:const char * strchr(const char *s, char c)


{


char c2;





for (const char *p = s; (c2 = *p) != 0; ++p)


if (c2 == c) return p;





return null;


}


I need help with pointers in C++?

Write a function that takes a C string as an input parameter and reverses the string. The function


should use two pointers, 'front' and 'rear'. The 'front' pointer should initially reference the first


character in the string, and the 'rear' pointer should initially reference the last character in the


string. Reverse the string by swapping the characters referenced by 'front' and 'rear', then increment


front to point to the preceding character, and so on, until the entire string is reversed. Write a


main program to test your function on various strings of both even and odd length.


*/


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





void swap_char(int* pFront, int* pRear);





int main(){


string sentence;


int *pFront, *pRear;


cout %26lt;%26lt; "Type in a sentence of less than 40 characers: \n";


cin %26gt;%26gt; sentence;


swap_char(pFront, pRear);


return 0;


}


void swap_char(int* pFront, int* pRear){


while(pRear %26gt; pFront)


{


string sentence;


char temp;


%26amp;temp =

I need help with pointers in C++?
Some tips:





pFront and pRear should be char*, not int*.





You need to set pFront and pRear to something before calling swap_char. However, the problem statement says swap_char's argument should be a "C string". I assume it wants you to use character array, and not a C++ string object. Inconvenient as that may be, you need this signature:





void swap_char(char * const s);





(i.e., const pointer, non-const data)





You might think you should have this call in main() :


swap_char(sentence.c_str());





That won't work, though, because c_str() returns a const char*, and swap_char needs to modify the character array. You need to declare a character array, then copy the c_str from sentence. Also, there's no need to limit the input to 40 characters, or any length.





So you need something like this:





getline(cin,sentence);


char *line = new char[sentence.length()+1];


memcpy(line,sentence.c_str(),sentence....


swap_char(line);





Why getline instead of cin's operator%26gt;%26gt;, you ask? Try them both, you'll see.





Also, #include %26lt;cstring%26gt; for memcpy. And when you're done, don't forget to: delete [ ] line;





I'll leave the guts of swap_char for you to work out, but this should get you going in the right direction.

survey research

About pointers in C?

hi..uhmm..about pointers in C.





char *s;


s="hello";





when u print the *s, the output is garbage or nonesense..


but if u print the value of s alone...hello would appear..why is this so? correct me if im wrong...


the s pointer points to the address of "hello" and when i dereference 8 it should work..by now...

About pointers in C?
I think you're doing it wrong.





You would need something like this:





#include %26lt;iostream%26gt;


using namespace std;





void main()


{


char *s;


char mystring[] = "hello";





s = mystring;





cout %26lt;%26lt; s;


system("Pause");


}





EDIT - I just tried this and it works, have a good day.
Reply:it won't print garbage .


it will just print h;


the pointer s will be just pointing to the first element .


so when you do a *s it will print h
Reply:its like this:





s stored the address location


*s stores the value stored in the first position of s


for printf and all functions that use string (i.e. char *) values, you pass the address and all manipulation etc happens to the value stored at address using address pointer -%26gt; pointer manipulation





when u print as:


printf("%s", *s); --%26gt; this should print junk





but when u print as:


printf("%c", *s); --%26gt; this should print the char at first byte location of s





and if u print as:


printf("%s", s); --%26gt; this should print the entire string stored in s





read a book on pointers, it will take too long to explain here. and its already explained very well in many books.





also there is a chance your current program (as it is) may core dump. this is because u r defining in one statement and then assigning in another. but since you did not allocate memory before proceeding to assign, your program will core dump. to resolve this, use:





char *s="hello";





doing this does definition, allocation of memory and assignation all at the same time.


Streams and pointers in C?

Are streams in C interchangable with pointers?





For example, can you use a stream to convert data of a string using a pointer address as the location?





How do streams work differently then pointers?

Streams and pointers in C?
stream : continous flow of data


pointer : address pointing to memory where data persent


Pointers in C++?

I'm learning about pointers in C++. I understand that to assign the address of var1 to the pointer var2, you





int var1;


int *var2;





var2 = %26amp;var1;





I saw the code below just now and I don't understand it.





int *i, j[10];


double *f, g[10];


int x;





i = j;


f = g;





for(x=0; x%26lt;10; x++)


cout %26lt;%26lt; i+x %26lt;%26lt; ' ' %26lt;%26lt; f+x %26lt;%26lt; '\n';





j[10] and g[10] are arrays so what happens if you


i = j;


f = g;


when I compile the code, it seems that the address of j is assigned to i and g to f. So why is there no %26amp;?

Pointers in C++?
i=j;





Is the same as





i = %26amp;j[0];





And believe it or not,





i = j + 5;





is the same as





i = %26amp;j[5];





That is why





cout %26lt;%26lt; i+x





Prints out the successive elements of the array.
Reply:*j or J[10] Basically in this 2 Variables your declaring that they're arrays





*j ---- doesn't have any specified size of array


J[10] ---- the size of array is 10


" POINTERS"in C?

Hi! frnds,


can anyone plz. sugest some sites on which i can get to solve some example programs in "POINTERS" in C , and also how to draw the flowchart and algorithm of a pointer program.please help....!!


thank u!

" POINTERS"in C?
See there are no specific sites for this but try some good books ,online www.cplusplus.com .
Reply:My name deepak I am doing Bca and site by site i have teach C language to Bca and Btech student.I have to tell


U because i don't thing that U have go for web site for


Pointer problem.It better to built up Ur confidence because


according to me if anybody want to learn C language(pointer).


First step is that U have more than Two Books at a


time.


Second step never read the theory directly move to program Because u r waisting Ur time in reading theory.


Third daily do only two to three question because if U completed whole exercise in one day u are totally confuse.


Forth Al way do first dry run mean try do solve the question in the copy first.


fifth than make the program.


Sixth always solve the error using top down approach.


seven finally U know the pointers.
Reply:There are no algorithms of "pointer programs" that I know of.





A pointer is basically a description of an address. For instance, and this is a bit contrived, but if you gave someone your house, they would have your house. But if you gave them the ADDRESS of your house, then they could still get the house if they want, but they wouldn't have to carry around the whole house.





Basically pointers are addresses of places in memory, so if you want to pass along a chunk of memory representing the text of a book, you can either pass along the whole text - which might be huge, and time-consuming to pass - or you could just pass the ADDRESS of the text. If the recipient needs to get at the text, it's a small leap once you have the address, and it saves passing all that information around, which chews up resources.
Reply:Hi,





I found the following sites with a very good examples of Pointers using C and C++





http://cis.stvincent.edu/html/tutorials/...





http://www.cs.cf.ac.uk/Dave/C/node10.htm...





http://www.augustcouncil.com/~tgibson/tu...
Reply:Hi,


if u use an under-dos C, maybe code below will explain lot of things to u:





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


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





#pragma hdrstop





int add1(int* a, int* b)


{


return *a + *b;


}





int add2(int%26amp; a, int%26amp; b)


{


return a + b;


}





constream c;





void main()


{


int a, b;


a = 1;


b = 6;


c.clrscr();


c %26lt;%26lt; add1(%26amp;a, %26amp;b) %26lt;%26lt; endl;





c %26lt;%26lt; add2(a, b);


getch();


}








HTH,





niccie_11
Reply:Pointers are actually poniting where you r going to save u'r information in c language, thus they cannot be shown on the flowchart or define using algorithm





i used www.borland.com, micrsoft sites to understand how i can use the pointers, the r deficult but if u try hard u well be able to understand them easily,

survey for money

String constant and pointers in C++?

Please look at the code below.





char *ptr;


ptr = "Pointers add power to C++.\n";


cout %26lt;%26lt; ptr;





Why does it print out "Pointers add power to C++"?





My book tells me that that "Pointers add power to C++" yields a pointer to its entry in the string table. So why doesn't the pointer print out the address of the string constant rather than the content itself?





So if the string constant is stored in the address 0x22ff50 shouldn't the program print out 0x22ff50?

String constant and pointers in C++?
If the string constant is stored at 0x22ff50, then what is actually stored in ptr is 0x22ff50. Fortunately for your sanity (and mine) cout is smart enough to know that when you do cout %26lt;%26lt; ptr you want to print the string at the address stored in ptr. Like the above answer says, if you want the address to print, you must cast it as a pointer to void - cout %26lt;%26lt; (void *)ptr;





This works through operator overloading. This answer is already too long, and explaining overloading would make it WAY too long. You will learn about it later, when you learn about classes. Basically, when you do cout %26lt;%26lt; myVar; what happens depends on what type myVar is.





Also, there is one more piece of magic that makes this work. The statement:


ptr = "Pointers add power to C++.\n";


is technically invalid. As you know, pointers store addresses, not strings! A compiler extension allows this statement to work. When you assign a string constant to a pointer to char, the compiler will allocate memory to store the string, put the string in that memory, and store the address of the first character in your pointer. Here is what happens behind the scenes: (not what actually happens, but a way to visualize it)





char *ptr;


//allocate memory to hold the string (28 chars)


//notice we need one extra char - the null character '\0' will be put at the end of the string.


char *aString = new char[28];


//store the string in memory


//You'll learn about pointer math shortly if you haven't already


*aString = 'P';


*(aString + 1) = 'o';


*(aString +2) = 'i';


*(aString + 3) = 'n';.


.


.


*(aString + 22) = 'C';


*(aString + 23) = "+";


*(aString + 24) = '+';


*(aString + 25) = '.';


*(aString + 26) = '\n';


//append the null character


*(aString + 27) = '\0';


ptr = aString;


cout %26lt;%26lt; ptr;


//at the very end of your code:


delete[] aString;





//end of diatribe, hope this helps...
Reply:This may vary by compiler, but at least with gcc, when you have





char *j = "hello";





the compiler returns an address to the static memory location of the string constant, not to a dynamically allocated array. The conversion from the string constant acts more like a "const char *". Report It

Reply:all other pointers except for character pointers shows address .


but character pointer acts as simple character array in c/c++
Reply:Pointers are typed -- meaning it is a char * pointer, not a "generic" pointer like a "void *" pointer. Thus, the compiler is aware you want a character pointer, and prints the string. If you were to cast this to a (void*) pointer it would not do the same thing -- even though you are pointing at the same memory location.