Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

character array

Posted on 1999-01-02
5
Medium Priority
?
248 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 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