|
-
March 3rd, 2009, 07:44 PM
#1
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
-
March 3rd, 2009, 08:12 PM
#2
Re: pointer
 Originally Posted by kiani45
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
-
March 3rd, 2009, 08:27 PM
#3
Re: pointer
This looks like C
 Originally Posted by kiani45
float* p ;
p = (float*) malloc( sizeof(float) * 10)
If you want to use C++
Code:
std::vector<float> p(10);
-
March 3rd, 2009, 08:40 PM
#4
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("%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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|