CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: pointer

  1. #1
    Join Date
    Mar 2009
    Posts
    9

    Angry pointer

    Hi, all

    If I have array:

    float* p ;
    p = (float*) malloc( sizeof(float) * 10)

    and I have a function that give me two numbers each time I call it

    If I want to call this function 5 times, how do I save these 10 value outputs sequentially into my array p buy just put my array into the function ?

    like:

    for( int i=0; i<5; i++ )
    {
    myfunction (A, B, *p) ;
    }



    THX

  2. #2
    Join Date
    Jan 2008
    Posts
    32

    Re: pointer

    Quote Originally Posted by kiani45 View Post
    Hi, all

    If I have array:

    float* p ;
    p = (float*) malloc( sizeof(float) * 10)

    and I have a function that give me two numbers each time I call it

    If I want to call this function 5 times, how do I save these 10 value outputs sequentially into my array p buy just put my array into the function ?

    like:

    for( int i=0; i<5; i++ )
    {
    myfunction (A, B, *p) ;
    }
    THX
    Code:
    #define ARRAY_SIZE 10
    int nIndex;
    float* p;
    void myfunction(float a,float b,float* p)
    {
     if (nIndex < ARRAY_SIZE-2) {
       p[nIndex]=a; nIndex++;
       p[nIndex]=b; nIndex++;
     }
    }
    
    int main()
    {
      nIndex=0;
      p=(float*)malloc(sizeof(float)*ARRAY_SIZE);
      ..............................
      myfunction(A,B,p);
      ..........
    }
    Have a good time

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: pointer

    This looks like C
    Quote Originally Posted by kiani45 View Post
    float* p ;
    p = (float*) malloc( sizeof(float) * 10)
    If you want to use C++
    Code:
    std::vector<float> p(10);

  4. #4
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: pointer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void myfunction(int &A, int &B, float *p)
    {
    	static a=0,b=0;
    	A = ++a;
    	B = ++b + a;
    
    	*p = A;
    	*(p+1) = B;
    }
    
    int main()
    {
    	float *p, *pOld;
    	p = (float*) malloc( sizeof(float)* 10 );
    	pOld = p;
    
    
    	int A,B;
    	for( int i=0; i<5; i++ ) 
    	{
    		myfunction(A, B, p);
    		p+=2;
    	}
    
    	p = pOld;
    	for( int j=0; j<10; j++ ) 
    	{
    		printf("&#37;f\n", *(p++));
    	}
    
            free(pOld);
    	return 0;
    }
    Last edited by ckweius; March 3rd, 2009 at 08:56 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured