?
Solved

character array

Posted on 1999-01-02
5
Medium Priority
?
242 Views
Last Modified: 2013-12-03
I'm having problem parsing an array of characters into a function and then
retreiving the character array after the function has completed.
Here is a sample example of what I am trying to do with ??? beside my problems..

I need my character array to be changed in the function.. but I'm not
sure how to do it..

do i need to use pointers and &'s or something?

Here's the example:

I have a class..

class Fred
{

public:

???      int billybob(char ?????);

};


****
main.cpp

int main() {


      Fred fred = new Fred();

      char anArrayOfChars[100];
      strcpy(anArrayOfChars, "hello, world!");

???      fred->billyBob(anArrayOfChars????);

      printf("%s\n", anArrayOfChars);

      return 0;

}
0
Comment
Question by:mramsay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:mramsay
ID: 1181416
anArrayOf characters will be changed in the function.. so it should'nt print out hello world
how do u do it
0
 
LVL 11

Accepted Solution

by:
alexo earned 400 total points
ID: 1181417
One thing to remember: an array is passed to a function as a pointer.  Therefore:

    class Fred
    {
    public:
        int billybob(char* array);
    };

and:

    fred->billyBob(anArrayOfChars);

You could use an array designation:

    class Fred
    {
    public:
        int billybob(char array[]);
    };

It is more readable but the information about the size of the array is lost anyway.  This is one of the "features" C++ inherited from C.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1181418
Here is exactly what you wanna do...

class Fred
   {
   public:

      void billyBob(char *str) {
         //The code for the function, if it's not too big...
      }
   };

   int main()
  {


      Fred fred;

    char anArrayOfChars[100];
    strcpy(anArrayOfChars, "hello, world!");

    fred.billyBob(anArrayOfChars);

   printf("%s\n", anArrayOfChars);

   return 0;

   }

Hope that helps...

-Viktor
--Ivanov
0
 

Author Comment

by:mramsay
ID: 1181419
Ok I'll try that out.

I noticed that Viktor typed:

Fred fred
and used
fred.billyBob

why do I see most classes declared like

Fred *fred
and used like
fred->billyBob

what is the difference?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1181420
The difference is that the one I used is a variable that the compiler allocates and deallocats the memory for, and the on you use is a pointer and you can allocate and deallocate the memory..

Using a pointer instead of a whole structures saves some memory...

Here is how to do it with a pointer also...

class Fred
        {
        public:

           void billyBob(char *str) {
              //The code for the function, if it's not too big...
           }
        };

        int main()
       {
           Fred *fred = new Fred;  //Declares a pointer to a class and allocates enough memory..

         char anArrayOfChars[100];
         strcpy(anArrayOfChars, "hello, world!");

         fred->billyBob(anArrayOfChars);  //When it's apointer you use a -> instead of . to access member functions or variables...

        printf("%s\n", anArrayOfChars);
        delete fred; //deallocate memory (free it)
        return 0;
        }

Hope this helps... If you need more info let me know...

-Viktor
--Ivanov
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question