i have written the code below which should print the value int the first array location
<CODE>
#include<stdio.h>
int arr(int *n)
{
int a[20],i;
printf("ENTER THE ELEMENTS OF ARRAY:");
for(i=0;i<*n;i++)
scanf("%d",&a[i]);
return a;
}
main()
{
int a[50],i,s,*val;
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&s);
val=arr(&s);
fflush(stdout);
printf("THE RETURNED ARRAY IS:%d",*val);
}
</CODE>
Though there are no errors,i am getting few warnings
retarr.c: In function ‘arr’:
retarr.c:8: warning: return makes integer from pointer without a cast
retarr.c:8: warning: function returns address of local variable
retarr.c: At top level:
retarr.c:11: warning: return type defaults to ‘int’
retarr.c: In function ‘main’:
retarr.c:15: warning: assignment makes pointer from integer without a cast
retarr.c:12: warning: unused variable ‘i’
retarr.c:12: warning: unused variable ‘a’
retarr.c:18: warning: control reaches end of non-void function
how to make the program print the value present in first location of the array,which i have given as input?
please help
thanks in advance
Referring to your original source code. - Use square brackets (not angle brackets) around your CODE tags.
- Function arr fills local array a[20]. It would very bad if arr were called with a parameter value greater than 20. This function should protect itself.
- By the way, what's the benefit of using call-by-reference instead of call-by-value in function arr? There's no harm in doing it your way, although you might want to make the argument a pointer-to-const to guarantee arr can't change the caller's parameter value.
- The definition of function arr says that it returns an integer, but you return an array of int, which actually returns the address of the first element of the array (that is, a pointer-to-int). This is the source of the first warning.
- Function arr returns the address of local array a. Local variables cease to exist once their enclosing function returns. This means that main is referencing ghost variables if it tries to dereference the returned pointer. That is undefined behavior -- there is no way to predict what will happen. This is the source of the second warning.
- You defined the main function without specifying its return type. Without an explicit return type, the compiler assumes the function returns an int. This is the source of the third warning. By the way, the return type mandated for main by the Standard is int.
- Function main assigns the return value of function arr to variable val. Function arr is defined to return an int, but variable val is a pointer-to-int. This discrepancy is the source of the fourth warning.
- Function main defines local variables a[50] and i, but never uses them. This is the source of the fifth and sixth warnings.
- Function main has [implicit] return type int, but execution falls through to the bottom of the function without encountering a return statement. What int value should it return? This is the source of the seventh warning.
9 2368
with your current development can only print the first data in INT array.
to print all you will have to follow loop
thanks a lot for ur reply....................
but the problem is,it is not even printing the first value
i tested your program in my machine. it print the first value.
well try this way. return ->
this code below will do what you're trying to do. I ran it in a C++ compiler just to make sure it was working. I tried to comment to help you out. Take a look, hope this helps ... -
#include<iostream>
-
#include<stdio.h>
-
-
//int arr(int *n)
-
void arr(int a[], int size) // need to pass an array AND the size of it
-
{
-
//int a[20]; don't need this
-
printf("ENTER THE ELEMENTS OF ARRAY:");
-
for( int i=0; i<size; i++ )
-
//scanf("%d",a[i]);
-
std::cin >> a[i];
-
//return a; // you can't return an array, must use passing by value
-
}
-
void main()
-
{
-
// int a[50]; // it's a good idea not to hard code the size of an array if you can avoid it
-
// int i; // not being used
-
int size = 0;
-
// int *val; // don't need this
-
printf("ENTER THE SIZE OF THE ARRAY:");
-
//scanf("%d",size);
-
std::cin >> size;
-
-
int* a = new int[size]; // here, a new, DYNAMICALLY allocated array is created
-
-
//val=arr(&s);
-
// instead of returning, you pass the array BY VALUE, along with the array's SIZE
-
// this allows the array 'a' to be changed in another function and everyone can
-
// see the changes
-
arr(a, size);
-
-
fflush(stdout);
-
-
for( int i = 0; i < size; i++ ) // added this to print ALL of the values
-
{
-
printf("THE RETURNED ARRAY IS:%d",a[i]);
-
}
-
-
int x;
-
std::cin >> x;
-
}
-
Referring to your original source code. - Use square brackets (not angle brackets) around your CODE tags.
- Function arr fills local array a[20]. It would very bad if arr were called with a parameter value greater than 20. This function should protect itself.
- By the way, what's the benefit of using call-by-reference instead of call-by-value in function arr? There's no harm in doing it your way, although you might want to make the argument a pointer-to-const to guarantee arr can't change the caller's parameter value.
- The definition of function arr says that it returns an integer, but you return an array of int, which actually returns the address of the first element of the array (that is, a pointer-to-int). This is the source of the first warning.
- Function arr returns the address of local array a. Local variables cease to exist once their enclosing function returns. This means that main is referencing ghost variables if it tries to dereference the returned pointer. That is undefined behavior -- there is no way to predict what will happen. This is the source of the second warning.
- You defined the main function without specifying its return type. Without an explicit return type, the compiler assumes the function returns an int. This is the source of the third warning. By the way, the return type mandated for main by the Standard is int.
- Function main assigns the return value of function arr to variable val. Function arr is defined to return an int, but variable val is a pointer-to-int. This discrepancy is the source of the fourth warning.
- Function main defines local variables a[50] and i, but never uses them. This is the source of the fifth and sixth warnings.
- Function main has [implicit] return type int, but execution falls through to the bottom of the function without encountering a return statement. What int value should it return? This is the source of the seventh warning.
thanks a lot everyone for your valuable replies
especially DONBOCK thanks a lot for your explanation of the warnings
but it did not help
am unable to get the output, i.e to print the complete array
am not getting any errors or warnings
this is the code -
#include<stdio.h>
-
int* arr(int *n)
-
{
-
int a[20],i,*add; //'add' is for storing the address of the first location in the array
-
printf("ENTER THE ELEMENTS OF ARRAY:");
-
for(i=0;i<*n;i++)
-
scanf("%d",&a[i]);
-
add= &a[0];
-
return add; //returning the address of the 'a[0]'
-
}
-
int main()
-
{
-
int s,i;
-
int *val;
-
printf("ENTER THE SIZE OF THE ARRAY:");
-
scanf("%d",&s);
-
val=arr(&s);
-
fflush(stdout);
-
for(i=0;i<s;i++)
-
printf("THE RETURNED ARRAY IS:%d",*val); //printing the complete array
-
return 0;
-
}
-
please help
thanks a lot in advance
- Refer to item #5 in my earlier post. You haven't fixed this yet. Note what I said: "there is no way to predict what will happen". You must fix this.
- Refer to the loop in lines 19 and 20 of your new source code. This loop prints the first entry in the array over and over again.
Thanks a lot for all,who helped me in my code development
i have figured a way to return the complete array it is working
heres the code i am posting
any enhancements needed?
please let me know
here is my code: -
#include<stdio.h>
-
-
//#include<conio.h>
-
-
int siddu(int *x)
-
-
{
-
-
int i;
-
-
int a[20];
-
-
//printf("enter the size of array:");
-
-
//scanf("%d",x);
-
-
fflush(stdin);
-
-
printf("ENTER THE ARRAY ELEMENTS:");
-
-
for(i=0;i<*x;i++){
-
-
scanf("%d",&a[i]);
-
-
//for(i=0;i<*x;i++)
-
-
return a[i];
-
-
}
-
-
}
-
-
int siddu2(int *x)
-
-
{
-
-
int a[20],i;
-
-
for(i=0;i<*x;i++){
-
-
scanf("%d",&a[i]);
-
-
//for(i=0;i<*x;i++)
-
-
return a[i];
-
-
}
-
-
}
-
-
void main()
-
-
{
-
-
int n,j,ret[20];
-
-
//clrscr();
-
-
printf("enter the no of sides:");
-
-
scanf("%d",&n);
-
-
for(j=0;j<n;j++){
-
-
if(j>0){
-
-
ret[j]=siddu2(&n);
-
-
continue;
-
-
}
-
-
ret[j]=siddu(&n);
-
-
}
-
-
fflush(stdout);
-
-
for(j=0;j<n;j++)
-
-
printf("%d",ret[j]);
-
-
//getch();
-
-
}
-
What do you intend to be the difference between siddu and siddu2? I don't see any practical difference between them. siddu calls fflush on stdin, but that is undefined behavior. The other difference is that siddu prints an annunciator string before entering the data entry loop.
Both siddu and siddu2 enter a data entry loop but return to the caller after the first pass through the loop. Why have code for a loop that you don't intend to execute? Similarly, why allocate a 20-entry array when you don't intend to use more than its first entry?
Line 51: it is illegal for main to return void. It must return int. If you change the definition on line 51 then you need to terminate the function with a return statement.
The loop between lines 63 and 75 has complicated logic that has the effect of calling siddu the first time through the loop and siddu2 for all other iterations. Presumably the reason for doing this is so the siddu annunciator string (line 19) is only printed once. It would be much easier to do that by moving line 19 to somewhere between lines 61 and 63 and simplifying the loop.
The essence of siddu and siddu2 is their calls to scanf (lines 23 and 41). What do you gain by having main call siddu/siddu2 rather than simply calling scanf directly?
Some C platforms have buffered output on stdout. If so, then all printed characters go to an internal buffer. They don't go to the output device until either fflush is called or a newline is printed. You should insert a line between lines 81 and 85 to print a newline (or move line 77 there).
The user does not have to enter what you expect. If the user types in something crazy then scanf will fail and the variable you are trying to load won't change. This would be especially bad if it happened at line 61. You should be checking the return code from scanf.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: BrianP |
last post by:
Hi,
I have had to invent a work-around to get past what looks like a
JavaScript bug, the malfunctioning Perl-like JavaScript array functions
including SPLICE() and UNSHIFT().
I have boiled it...
|
by: Marc van Boven |
last post by:
I'm stuck with the following problem:
My nusoap-client calls a server-function giveCombination(). The
function giveCombination should return something like
array(
=> array(
'a_id' => 6,...
|
by: priya |
last post by:
Hi all,
I am new to this group.I am working in c language.I have dount
in pointer? how to retun array of pointer in function?
example
main()
|
by: kathy |
last post by:
how to return array from function?
|
by: Ali |
last post by:
hi i need to send some data to the function and want to return array of object bac
lik
dim obj() as myclass=AddIt(i,j
i dont know that is write syntax or no
public function addit(byval i as...
|
by: DWalker |
last post by:
VBA, as used in Excel 2000 and Excel 2003, has a function called Array.
It's commonly seen in statements like the following:
Workbooks.OpenText Filename:=ACFileName, _
StartRow:=1,...
|
by: MLH |
last post by:
How would I modify the following to achieve a 2-dimensional array?
Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1...
|
by: Raj |
last post by:
I need a VB function to return array of collections like
Private Type Employee
empname as string
address as string
salary as integer
deptno as integer
End Type
dim employees() as Employee
|
by: =?Utf-8?B?RGFya21hbg==?= |
last post by:
Hi,
I am wondering how you multi-dimension an array function?
My declared function looks like this:
Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long,
ByVal Val3 As...
|
by: tyv |
last post by:
Hi,
I encountered a bug on a website that I am working on. The enter/
return key function works in certain parts of the website and doesn't
work in other parts. I went through all the scripts...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |