Sign in to follow this  
atomicajay

Please help me in pointer

Recommended Posts

atomicajay    100
I want to print 5 in cout but it is generating error help me please i am complete newbie
#include <iostream>
#include <string>

using namespace std;
int* geppSize(string* const text2);

main()
{
      string text1 = "Hello";
      int *pgetsize = geppSize(&text1); //passing hello to function
      cout << "Size of text:" << *pgetsize << endl;
      system("pause");
      return 0;
}
int* geppSize(string* const text2)
{
 int* a = (*text2).size();
 return &a;
}

Share this post


Link to post
Share on other sites
namar777    488
Anytime you get errors, post them.

You haven't allocated space for integer! Also, bad practice to allocate something for a primitive (unless you need a changing array or something). But following your code you do that in the geppSize function.

Like So:



int* geppSize(string* const text2)

{
int* a = (int*)malloc(sizeof(int));
a = (*text2).size();
return a; //don't need to reference pointer, you'll have a double pointer!
}

//don't forget to deallocate it in main at the end.

free(pgetsize);





Keep at it

Share this post


Link to post
Share on other sites
mattd    1078
test.cpp: In function 'int* geppSize(std::string*)':
test.cpp:17: error: invalid conversion from 'size_t' to 'int*'
test.cpp:18: error: cannot convert 'int**' to 'int*' in return

Firstly, you're trying to store the result of std::string::size, a size_t, into a int *. This isn't going to work - the two are different types, with no suitable conversion between them. You can fix this by using a size_t variable instead.

Secondly, you're returning a int ** (pointer to a pointer to a int), and not a int * as specified from geppSize. You could fix this by just returning a as is, but this ignores the first problem, namely that a isn't actually a valid pointer, just the result of std::string::size converted into a pointer.

You really want to return just a straight size_t from geppSize, not a pointer. There is nothing to be gained from doing so.

If you did want to return a pointer to the result, you would do something like:

int* geppSize(string* const text2)
{
int* a = new int;
*a = (*text2).size();
return a;
}

Note that the burden of ownership for the pointer is returned to the caller - the caller has to remember to eventually delete it!

A few other points:

  • You need to specify a proper definition for main - while 'main()' is often accepted by compilers, it's not standard C++. Use a full definition with return value, like so: "int main()"
  • Try to find a method of keeping the result visible from within your IDE/console itself, instead of using the system-specific system("pause");.
  • I'm guessing you haven't covered references in your studies yet, but they'd be ideal for the parameter to geppSize. I'll leave them out of my example.
  • You can simplify an expression like (*x).y to the nicer x->y.


Combining these points, we get something like:
#include <iostream>
#include <string>

using namespace std;

size_t geppSize(string* const text2);

int main()
{
string text1 = "Hello";

size_t size = geppSize(&text1); //passing hello to function

cout << "Size of text:" << size << endl;

return 0;
}

size_t geppSize(string* const text2)
{
return text2->size();
}




namar777: C++, not C :)

Share this post


Link to post
Share on other sites
Zahlman    1682
Except that there is also no reason to use a pointer to pass the parameter, and that we don't need a prototype if we simply put the helper function before main(). Finally, the const-correctness wasn't as desired: we want to say that the string won't be changed - we don't care about the pointer (or reference; but a reference can't be changed anyway).

Oh, and there is no need to keep the parameter names unique. Please learn about how variable scoping works. Actually, we don't even need to name everything in the program.

If I were the teacher, I wouldn't accept anything more complicated than:


#include <iostream>
#include <string>

using namespace std;

// Simple demonstration of passing a string to a function in C++.

size_t lengthOf(const string& text)
{
return text.size();
}

int main()
{
cout << "Size of text:" << lengthOf("Hello") << endl;
}



because every extra complication just suggests a lack of understanding.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this