473,395 Members | 1,869 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,395 software developers and data experts.

Regarding Function

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4.    int a[] = {345,342,784,1223,1,775,3,1,234};
  5.    int b[] = {};
  6.    int x,*y,m;
  7.    x = sizeof(a)/sizeof(a[0]);
  8.    y = malloc((int *)sizeof(a));
  9.    m = int * arrayreturn(*b);
  10.    printf("%d\n",x);
  11.    printf("%d\n",y);
  12.    printf("%d\n",m);
  13.    printf("%d",y[0]);    
  14.    getch();
  15. }
  16.  
  17. int arrayreturn(int *b)
  18. {
  19.    int x = 3;
  20.    return x;
  21. }
can anybody tell me whats the error in above program?
Apr 5 '08 #1
1 1107
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4.    int a[] = {345,342,784,1223,1,775,3,1,234};
  5.    int b[] = {};
  6.    int x,*y,m;
  7.    x = sizeof(a)/sizeof(a[0]);
  8.    y = malloc((int *)sizeof(a));
  9.    m = int * arrayreturn(*b);
  10.    printf("%d\n",x);
  11.    printf("%d\n",y);
  12.    printf("%d\n",m);
  13.    printf("%d",y[0]);    
  14.    getch();
  15. }
  16.  
  17. int arrayreturn(int *b)
  18. {
  19.    int x = 3;
  20.    return x;
  21. }
can anybody tell me whats the error in above program?
There are quite a lot, actually:

1) You are using main(), which is not advisable. Instead, write int main(), and at the very end, write return 0; This is the standard practice.

2) Using the sizeof() tricks to find the size of the array works here...but only here. Don't rely on that trick to get the size of an array in functions...in fact, it's better to get used to hardcoding a size for your array (i.e. a global constant variable SIZE).

3) Your malloc call is incorrect. malloc returns a void*, and your code expects an int*. I suppose that's why you included the typecast, but the place you put it is completely incorrect. That takes the size of a and casts it to a pointer, then tries to make that many bytes of memory - not going to work.

4) Just because you get memory for y (assuming you fix the malloc call) doesn't mean it has meaningful values, so printing out y[0] won't give you any useful information.

5) Your function call is incorrect - in the call, you don't include the return type. If you do, C will think you are declaring a new function and probably complain.

6) Your argument to arrayreturn is incorrect - you pass it *b (which is an int, since b is an int*), but the function expects an int*. You should be passing b.

7) Your function doesn't actually use the argument, so what's the point?

This function has a lot of flat out wrong code - are you diving too deep into programming? You may want to study the syntax of these simple manipulations before trying any pointer manipulation or function calls.
Apr 5 '08 #2

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

Similar topics

1
by: Mosas | last post by:
Dear All, Php has exec function to run system commands and this function return last line of system command result. But in Python I can see only os.system() function to run system command but it...
8
by: gg | last post by:
I am confused regarding what the line in the following function does. It seems to work ok. It seems to be creating a new T object using the pointer to an existing T object. But which function is it...
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
0
by: sonu | last post by:
Hi all, I have a problem regarding use of manualreset events in .net. I have a developed a sample appliation in which entries from the listview which I say is a queue for my processes.I want to...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
10
by: sam_cit | last post by:
Hi Everyone, I had a doubt regarding extern decleration, i tried this is one source file, extern int sample; extern int sample; int main() {
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
1
by: bg_ie | last post by:
I'm designing a database with 3 tables called Function, Test and Scene. A Function has multiple Tests, but a Test has only one Function. A many to many relationship exists between Test and Scene...
5
by: Philip Potter | last post by:
I have a somewhat flippant question regarding undefined behaviour. Does an operation which invokes undefined behaviour affect the whole program, or are earlier statements guaranteed to execute...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.