Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,548 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,561 people online right now. Registration is fast and FREE... Join Now!




Again Related to Pointers

 
Reply to this topicStart new topic

Again Related to Pointers

garima
29 Oct, 2007 - 11:40 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
i need to do the following program with the help of pointers....please help me out....
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[80];
    cout<<"Enter string: ";
    cin.getline(str,80);
    int a =strlen(str);
    int i,j;
    for(i=0,j=a-1;i<a/2;i++,j--)
    {
        str[i] = str[i] + str[j];
        str[j] = str[i] - str[j];
        str[i] = str[i] - str[j];
    }
    cout<<"\nReverse string is: ";
        for(i=0;i<a;i++)
            cout<<str[i];
            cout<<"\n";
    return 0;
}


i tried doing it this way...it's working also but now i need to pass on the reversed string to another function.....this is something i simply dont know....help me out in this:

CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void reverse( char *str );

int main()
{
  char s[80];
  cout<< "Enter the string: ";
  cin.getline(s,80);
  reverse(s);
  cout<<"\n";
  return 0;
}
void reverse( char *str )
{
      if(str[0]=='\0')
      {
        return;
      }
      else
      {
       reverse(&str[1]);
       putchar(str[0]);
      }
}  


This post has been edited by jjhaag: 29 Oct, 2007 - 11:59 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Again Related To Pointers
30 Oct, 2007 - 12:10 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
To use pointers to traverse/manipulate a c-string, keep in mind that *(array+i) can be used much the same as you would use array[i]. In fact, this is basically what the [] operator does - it performs a pointer arithmetic step using the base address of the array, then a dereferencing step to access a particular element.

Any time you pass a string to a function with a pointer argument, you're modifying the original string. So you can pass that same string on to a new function much as passed it in the first place.

And you don't need to create a new post when dealing with the exact same code. If you don't understand something or have further problems with your code, just reply in the original thread.

-jjh
User is offlineProfile CardPM
+Quote Post

garima
RE: Again Related To Pointers
30 Oct, 2007 - 12:42 AM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
i am really sorry but i didnt got anything.......i am very weak in concepts of pointers...i understand them but i dont get it very easily as to how to implement them.....so please help me..........
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Again Related To Pointers
30 Oct, 2007 - 01:10 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You need to be more clear with your questions. The only way that you're going to receive help is if you make it possible for us to help you. We appreciate it when members are polite, and say please when posting questions, but when the entire post consists of not much more than the source code and "please help", there's not much we can do. In that case, the only thing that would really clear up your problem would be to write your assignment for you, but that's in violation of forum policy.

That being said, have you tried to implement the suggestion in the post above? i.e. have you actually gone into your code and tried replacing the array subscript notation (e.g. str[i]) with the pointer arithmetic notation (e.g. *(str+i))? The only way to learn a language is to actually get in there and try it.

And I would also recommend that you work through the tutorial on pointers by Dark_Nexus - and actually work through it, trying out the examples to see how they work, rather than just reading it. The examples are clear and well thought out, and they are short and simple enough that they can be worked through quickly and easily.

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

garima
RE: Again Related To Pointers
30 Oct, 2007 - 01:39 AM
Post #5

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
but when i am using
CODE
*[str+0]
instead of
CODE
str[0]
then this is giving an error....whot should i do now...
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Again Related To Pointers
30 Oct, 2007 - 01:55 AM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Look again - for dereferencing a pointer arithmetic expression, those should be parentheses like this *(str+i), not brackets like this *[str+i].
User is offlineProfile CardPM
+Quote Post

garima
RE: Again Related To Pointers
30 Oct, 2007 - 02:04 AM
Post #7

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
fine.....i did that and it's working but please do tel me how to store this result of reversed string so that i can pass it into a different function of same program....
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Again Related To Pointers
30 Oct, 2007 - 03:13 AM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(garima @ 30 Oct, 2007 - 03:04 AM) *

fine.....i did that and it's working but please do tel me how to store this result of reversed string so that i can pass it into a different function of same program....

I don't want to sound rude but you need to get some book or start reading some tutorials, first. Then if things are not clear, ask questions, and try to be more specific and concrete. Thanks.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month