Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Store int* array

Reply
Thread Tools

Store int* array

 
 
MN
Guest
Posts: n/a
 
      08-27-2008
How to store the function's result into variable in this case?
My code is like this::

#include <stdio.h>
#include <stdlib.h>

extern int* var1;

int* function(int in1, int in2);

int* var1 =NULL;
int* view_var1 =NULL;

int main ()
{
int i = 0;
//extern int* view_var1;
int in1 = 5;
int in2 = 10;

/* if (view_var1 != NULL) */
/* free(view_var1); */

/* view_var1 = (int*)malloc (sizeof(int*)* in2); */

/* view_var1 = function(in1, in2); */

function(in1, in2);

for (i = 0; i < in2; i++)
{
if (i == 0)
//printf("- %d\n", &view_var1[i]);
printf("- %d\n",var1[i]);

else
//printf("%d %d\n", i-1, &view_var1[i]);
printf("%d %d\n", i-1, var1[i]);
}
return 0;

}

int* function(int in1, int in2)
{
int i = 0;
if (var1 != NULL)
free(var1);
var1 = (int*)malloc (sizeof(int*)* in2);


for (i = 0; i < in2; i++)
{
if (i == 0)
var1 [i] = 0;
else
{
if (i == 1)
var1[i]= 1;
else
var1[i] = var1[i-1]*2+in1;
}
}
return (var1[in2]);
}


The right result must be :
- 0
0 1
1 7
2 19
3 43
4 91
5 187
6 379
7 763
8 1531

But if I activate lines that use view_var1, I get a result (which is
wrong) like this:
- 0
0 4
1 8
2 12
3 16
4 20
5 24
6 28
7 32
8 36

Also how to do in order to avoid these warning?
test.c:26: warning: return makes pointer from integer without a cast
test.c:56: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘int *’



 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      08-27-2008
On Wed, 27 Aug 2008 00:07:35 -0700, MN wrote:
[snip]
>
> for (i = 0; i < in2; i++)
> {
> if (i == 0)


{

> //printf("- %d\n", &view_var1[i]);
> printf("- %d\n",var1[i]);


}
>
> else


{

> //printf("%d %d\n", i-1, &view_var1[i]);
> printf("%d %d\n", i-1, var1[i]);


}

> }
> return 0;
>
> }
>
> int* function(int in1, int in2)
> {
> int i = 0;
> if (var1 != NULL)
> free(var1);
> var1 = (int*)malloc (sizeof(int*)* in2);
>
>
> for (i = 0; i < in2; i++)
> {
> if (i == 0)
> var1 [i] = 0;
> else
> {
> if (i == 1)
> var1[i]= 1;
> else
> var1[i] = var1[i-1]*2+in1;
> }
> }
> return (var1[in2]);
> }
>


The return type is int* but here you return int. The correct
way would be to write:

return var1+in2;

But then again, that just returns a pointer pointing beyond the
allocated array. I'm guessing you actually want the beginning
of the array:

return var1;

--
OU
 
Reply With Quote
 
 
 
 
MN
Guest
Posts: n/a
 
      08-27-2008
Thanks for your help.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-27-2008
MN <(E-Mail Removed)> wrote:
> How to store the function's result into variable in this case?
> My code is like this::


> #include <stdio.h>
> #include <stdlib.h>


> extern int* var1;


Why do you declare it as extern if you define it just
two lines later?

> int* function(int in1, int in2);


> int* var1 =NULL;
> int* view_var1 =NULL;


> int main ()
> {
> int i = 0;
> //extern int* view_var1;
> int in1 = 5;
> int in2 = 10;


> /* if (view_var1 != NULL) */
> /* free(view_var1); */


> /* view_var1 = (int*)malloc (sizeof(int*)* in2); */


'view_var' seems to be supposed to be a pointer to int, at
least that's how you defined it. Then this line is wrong,
since you allocate room for 'in2' pointers to int, and
not for 'in2' ints. You also shouldn't cast the return
value of malloc() - the conversion from void pointer to
the left hand side type happens all by itself, but the
cast will keep the compiler from warning you when you
forget to include <stdlib.h>. So make that line

view_var = malloc( in2 * sizeof( int ) );

or, perhaps better

view_var = malloc( in2 * sizeof *view_var );

You may have got away with your faulty version since on
a number of systems an int and a pointer to int take
the same amount of room, but that's nothing you should
rely on.

> /* view_var1 = function(in1, in2); */


> function(in1, in2);


> for (i = 0; i < in2; i++)
> {
> if (i == 0)
> //printf("- %d\n", &view_var1[i]);


'&view_var1[i]' is a pointer to the element with index 'i'
of the 'view_var' array and not an integer. Simply drop
the '&'.

> printf("- %d\n",var1[i]);


> else
> //printf("%d %d\n", i-1, &view_var1[i]);


Same her.

> printf("%d %d\n", i-1, var1[i]);
> }
> return 0;


> }


> int* function(int in1, int in2)
> {
> int i = 0;
> if (var1 != NULL)
> free(var1);
> var1 = (int*)malloc (sizeof(int*)* in2);


Here's the same problem as above, you need

var1 = malloc( in2 * sizeof *var1 );

> for (i = 0; i < in2; i++)
> {
> if (i == 0)
> var1 [i] = 0;
> else
> {
> if (i == 1)
> var1[i]= 1;
> else
> var1[i] = var1[ i - 1 ] * 2 + in1;
> }
> }
> return (var1[in2]);


This will return an int, not, as the function is defined to,
a pointer to int. I guess you wanted to do

return var1;

> }

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Store an array to an integer array?? rhitz1218@gmail.com C Programming 7 12-12-2006 12:43 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments