passing pointers of array to function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: passing pointers of array to function

  1. #1
    Join Date
    Feb 2009
    Posts
    6

    passing pointers of array to function

    Hello everyone,
    ive been googleing this all day and havent found an answer yet.
    i want to create an array in int Main() and then pass a pointer referencing the
    array to a function to fill it.
    This is how far i have gotten:
    Code:
    int main(void)
    {
    	test();
    	float dat[1000];
    	readnprint(&dat[0]);
           /*
             other code her
            */
    }
    
    void readnprint(float *base_ptr)
    {
        //init ifstream etc etc..
             float addr=base_ptr;
    	while (!ins.eof())
    	{
    		ins>>x;
    		float f;
    		if(from_string<float>(f, string(x), dec)) //parse float?
    		  {
    			addr=(base_ptr)+(sizeof(float)*cnt)           //steps through array by sizeof(float)
    			*(float)addr=f;  //have this assign the value to memory location (problem)
    		  }
    		  else
    		  {
    			cout << "from_string failed" << endl;
    		  }
    		cnt++;
    	}
    	ins.close();
    }
    im sure im just tired and not thinking right, does anyone have a better/working way of doing this?

    thanks

    -maze

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: passing pointers of array to function

    Quote Originally Posted by madmaze View Post
    im sure im just tired and not thinking right, does anyone have a better/working way of doing this?
    Why are you working with arrays? A proper container (e.g., std::vector) is more robust and safer.
    As for the other part of your problem, this is easily solvable with std::generate and a functor.

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: passing pointers of array to function

    This is one way of doing it.

    Code:
    void main()
    {
       float dat[1000];
       readnprint(dat, 1000);
    }
    
    void readnprint(float* base_ptr, int size)
    {
       for (int i = 0; i < size; ++i)
       {
          base_ptr[i] = i;
       }
    }
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,358

    Re: passing pointers of array to function

    Quote Originally Posted by _Superman_
    This is one way of doing it.
    Note that void main() should be int main(), as in madmaze's original example.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2009
    Posts
    6

    Re: passing pointers of array to function

    not sure what i was thinking last night.. simply passing by ref is the easiest..
    thanks for the responses =)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center