473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Passing and returing a pointer in function

I am trying to pass a pointer of an array in function. In function, i am trying to reverse the array elements.After that, pointer will return to main() function. In rev() function it prints perfectly alright but in main() it only print 1st element of array and then print some garbage value.Please solve my problem.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. int *rev(int *p);
  5. main()
  6. {
  7. int a[5],*x,i,a2[5],*t;
  8. clrscr();
  9. x=a;
  10. //t=a2;
  11. printf("\n enter the no");
  12. for(i=0;i<=4;i++)
  13.  
  14. scanf("%d",(x+i));
  15. t=rev(x);
  16. for(i=0;i<=4;i++,t++)
  17. printf("\n%d",*t);
  18. getch();
  19. }
  20. int *rev(int *p)
  21. {
  22. int *r,i,a1[5],j;
  23. r=a1;
  24. p=p+4;
  25.  for(i=0;i<=4;i++,r++,p--)
  26. {
  27. r=p;
  28. printf("\n%d",*r);
  29.  }
  30. r=r-5;
  31. printf("\n%d",*r);
  32.  return r;
  33.   }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
Oct 11 '07 #1
1 1235
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  
  2. int *rev(int *p)
  3. {
  4.   int *r,i,a1[5],j;
  5.  
  6.   r=a1;
  7.   p=p+4;
  8.  
  9.   for(i=0;i<=4;i++,r++,p--)
  10.   {
  11.     r=p;
  12.     printf("\n%d",*r);
  13.   }
  14.  
  15.   r=r-5;
  16.   printf("\n%d",*r);
  17.  
  18.   return r;
  19. }
  20.  
You problem is here in the function rev, you return r and r points to the array a1. However a1 is an automatic storage class variable which means it is created on the program stack and that in turn means it is deleted when the execution returns from the function so all the data in it is lost.

I would suggest that rev should take 2 pointers, 1 to the array with the data and 1 to the array to store the reversed data. You probably ought to pass the number of elements in the array into rev as well rather than assuming it is 5.
Oct 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.