Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

i think i have a logic fault here.. any idea?

#include<stdio.h>
 int sayi(void){
       int b[]={0,1,2};
       return *b;
         }
 main(){
       int i,*x;
       *x=sayi();
       for(i=0;i<3;i++){
            printf("%d\n",*(x+i));
            }
       return 0;

      }

I want to get b array what is my fault?
0
barisuzun
Asked:
barisuzun
  • 4
  • 3
  • 2
1 Solution
 
gj62Commented:
A bunch of things.

First, since int b[]... was done inside your function, it was allocated on the stack, which disappeared after your function call, leaving you an invalid pointer.

Second, sayi is supposed to return an int, not int *.

Third, you say *x=say(i) - but x is already a pointer to an int, so what you said was a pointer to a pointer to an int.

So try the following...  One item of note - I really don't know what you want to accomplish, but there is a particularly bad programming habit in the code below, and that is that a function allocates memory that must be freed outside the function body (if not, you'll leak memory each time you call sayi().  Not really what you want to do, but I wanted to be as true to your original code as possible...

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

int * sayi(void)
{
      int *b;
       b = (int*)calloc(3, sizeof(int));
       b[0] = 0;
       b[1] = 1;
       b[2] = 2;

      return b;
}
main()
{
      int i,*x;
      x=sayi();
      for(i=0;i<3;i++)
       {
           printf("%d\n",(x[i]));
      }
      free(x);
      return 0;

}
0
 
catbuttCommented:
Try:

int *sayi(void){
      int b[]={0,1,2};
      return b;
      }


You need to return a pointer to an int, not an int.

0
 
gj62Commented:
A few comments, by the way...

int * sayi(void) - return a pointer to an int (or an array of ints)...

int *b;  - b is now a pointer to an int (or array of ints)

b = (int*)calloc(3,sizeof(int)); - allocates enough memory for 3 ints, returns a pointer to an int (rather than a void pointer, which is what calloc would return with the cast of (int *)...

x = sayi(); - x is now a pointer to an array of ints (you wouldn't necessarily know how much unless you know what is going on in sayi() - so not the best programming practice.

free(x) - frees the memory allocated in sayi(), avoiding a memory leak...

Hope this helps...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
barisuzunAuthor Commented:
thanx for any any help
0
 
barisuzunAuthor Commented:
thanx for any any help
0
 
catbuttCommented:
gj62 said: "First, since int b[]... was done inside your function, it was allocated on the stack, which disappeared after your function call, leaving you an invalid pointer."

Are you sure that's true?  Since the array is given constant contents, it should exist for the life of the program, so it seems like it should be ok (aside from the fact that it needs to return a pointer as we both mentioned).

Admittedly, I never do what he's doing here, but I do this kind of thing fairly often:

char *s = "hello";
return s;

which is perfectly ok, and it seems to me to be the same as:

char s[] = {'h', 'e', 'l', 'l', 'o', '\0'};
return s;

(which is basically what he did, except with chars rather than ints)
0
 
barisuzunAuthor Commented:
Hey thanx gj62 it is working.. But can you teach me something about what you did. we have a pointer function (*sayi()) ant this function returns to a int. in fact what is a pointer function?  
0
 
barisuzunAuthor Commented:
Hey thanx gj62 it is working.. But can you teach me something about what you did. we have a pointer function (*sayi()) ant this function returns to a int. in fact what is a pointer function?  
0
 
gj62Commented:
Catbutt - it is different for char * than int *.  If I try and compile your statement, I get warning C4172: returning address of local variable or temporary

Barisuzun - a "pointer function" is nothing more than a function that returns a pointer of some type - in our example, it is a pointer to an integer.  What is happening is that you are receiving a pointer (reference) to the value, rather than the actual value itself.  This is commonly done with the string manipulation functions in the C runtime, such as strcpy...  Hope that helps
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now