?
Solved

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

Posted on 2003-02-22
9
Medium Priority
?
216 Views
Last Modified: 2010-04-15
#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
Comment
Question by:barisuzun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 165 total points
ID: 7999984
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
 
LVL 1

Expert Comment

by:catbutt
ID: 7999990
Try:

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


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

0
 
LVL 6

Expert Comment

by:gj62
ID: 7999992
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:barisuzun
ID: 7999996
thanx for any any help
0
 

Author Comment

by:barisuzun
ID: 8000021
thanx for any any help
0
 
LVL 1

Expert Comment

by:catbutt
ID: 8000022
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
 

Author Comment

by:barisuzun
ID: 8000060
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
 

Author Comment

by:barisuzun
ID: 8000083
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
 
LVL 6

Expert Comment

by:gj62
ID: 8000667
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question