Login Register


C programming array to function filter_list
Author
Message
C programming array to function #1
hello guys, the source below is related to array and function..in which entire declared array get passed to a function called show and in this function each number get multiplied by 3 and after that print outs new modified number.

Code:
main() { int n[]={1,2,3,4,5}; int *r,i=0; clrscr(); show(&n[i],5); r=&n[i]; while(i<5) { printf("%d\t",*r); ++r; ++i; } getch(); } int show(int *j,int n) { int i; for(i=0;i<n;++i) { *j=*j*3; ++j; } }

if you faced any problem feel free to ask
[Image: 262s21x.jpg]


RE: C programming array to function #2
This is one ugly "C" code (I'm putting that into sarcastic quotes, because it's not really C by the standard). Please try to learn and understand the language better before teaching others, because it's just striking how can you make so many mistakes (and not even small ones) in such short piece of code and it doesn't even do what it says that it does and you're teaching something like that to others?! Also the code won't even compile under many sane compilers.

1) You're missing headers. Never omit headers, while some compilers might add them automatically, it's not standard behavior and it's poor practice to do. Write your code like a good programmer would.

2) Learn to indent your code, it's pain to read poorly formatted code.

3) main function is missing return type. Don't rely on defaulting to int, it's outdated. Keep up with the technology and slap "int" in front of it, otherwise it's not proper C according to latest standard. And bad programming practice in addition to that.

4)
Code:
clrscr();
What's the meaning of this? First, it's not standard C function and second and more importantly, why are you clearing the screen? Program shouldn't do something user doesn't expect it to do, you should use something like that only when it's crucial to the code, which it isn't, it's unnecessary clutter. Get rid of it. Fast.

5)
Code:
&n[i]
This bit is a bit pointless. You write n, which is a pointer to the first element of the array. Then you deference it with to get the first element of the array and then you get its address with & to get the pointer to the first element of the array. Simply put, you need an apple, so you took apple, turned it to orange and then turned it to apple again. Why not just leave it apple?
Code:
show(n,5);

6) What do you need the r pointer for? It's completely useless. Just use array indexing, you have the array right there, why do you have to print its contents in such overcomplicated way? Also "while" loop shouldn't be used here, if you know the number of repeats, always use for, it's a good programming practice to do it that way, which results in clean, readable and manageable code.
Code:
for(i = 0; i < 5; i++) printf("%d\t",n[i]);
Simpler isn't it? Even faster and cleaner.

7) Okay... Ooookaaay... I don't get this. Why is a function which [i]multiplies
called show? Seriously why? Function should be called after what it does, not something else, because when I call function named show, I expect it to... well... show something, which yours doesn't. It's a horrible thing to do and sign of very bad code. Remember: Always name functions, classes and variables after what they're do, give them meaningful names to make the code readable, understandable and more professional..

8) You declared the function show as returning int, but you don't return anything. Why? This code either won't compile, or it will return some garbage from the memory. In either way, it's not very good. Easiest thing is to make it void.

9) Same with main. You didn't specify type, which defaults to int with old versions of C, but again, you're not returning anything. Well it will return something if it compiles, but it won't be nice.

10) You're missing prototype of the function show. It won't compile with any normal compiler, because inside of main, it doesn't know about function show yet, so how can it reference it? Either put it in front of the main or put a prototype in front.

11) Learn to comment your code.

This is the example rewritten by me, with all the corrections:

Code:
#include <stdio.h> // function prototype void multiply_by_three(int *array, int n); int main() { // initialize the array int n[] = {1,2,3,4,5}; int i; // multiply all elements by thre multiply_by_three(n, 5); // print the contents of the array for(i = 0; i < 5; i++) printf("%d\t", n[i]); // wait for the user to press enter getchar(); return 0; // return exit success } // function definition void multiply_by_three(int *array, int n) { int i; for(i=0; i<n; i++) array[i] *= 3; // multiply the element by three }
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.


RE: C programming array to function #3
Frooxius made a lot of great comments about your code and you should follow them because it will make your source code easier to understand. I made the bare minimum changes to your code in order to successfully compile it using gcc. When writing code you should code it in such a way that it would be easy to modify. I suggest that you don't hard code the number of elements into your code like:

Code:
show(&n[i],5);

Code:
while(i<5);

Instead, use variable and the built-in function sizeof().

Code:
int k=sizeof(n)/sizeof(int);

so if the number of elements in the array changes you don't need to change anything.

Code:
#include <stdio.h> main() { int n[]={1,2,3,4,5,6,7,8,9}; int *r,i=0; //clrscr(); int k=sizeof(n)/sizeof(int); show(&n[i],k); r=&n[i]; while(i<k) { printf("%d\t",*r); ++r; ++i; } //getch(); } int show(int *j,int n) { int i; for(i=0;i<n;++i) { *j=*j*3; ++j; } }

Don't stop coding and you will definitely improve. I'm learning c as well so you can ask me for help anytime.


RE: C programming array to function #4
You shouldn't declare the main() function with no return type. In older versions of C/C++ when no type is specified it defaults to the type int, but if you don't put there any explicit return statement, it will return some garbage value from memory.

Newer versions don't even allow you to do that anymore (it's a compile time error). The compilers still might accept it and generate a warning, but if you use more strict settings, they won't compile such code.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.


RE: C programming array to function #5
Hello Everyone,
Really good information and conversation regarding Array function in C language,
Thanks all of you for sharing information.


RE: C programming array to function #6
clrscr(); is a borland thingy and it wont be compiled in other compilers. Use system("cls"); as it is compatible in most of the compilers.

btw clrscr(); / system("cls"); clears the whole previous outputs and it is kind of useful according to me.


RE: C programming array to function #7
clrscr(); is a borland thingy and it wont be compiled in other compilers. Use system("cls"); as it is compatible in most of the compilers.

btw clrscr(); / system("cls"); clears the whole previous outputs and it is kind of useful according to me.


RE: C programming array to function #8
(01-12-2013, 07:48 AM)chemicalrage Wrote: clrscr(); is a borland thingy and it wont be compiled in other compilers. Use system("cls"); as it is compatible in most of the compilers.

btw clrscr(); / system("cls"); clears the whole previous outputs and it is kind of useful according to me.

system() should not be used... http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: C programming array to function #9
(01-12-2013, 07:48 AM)chemicalrage Wrote: clrscr(); is a borland thingy and it wont be compiled in other compilers. Use system("cls"); as it is compatible in most of the compilers.

btw clrscr(); / system("cls"); clears the whole previous outputs and it is kind of useful according to me.

system() should not be used... http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: C programming array to function #10
(01-12-2013, 07:48 AM)chemicalrage Wrote: clrscr(); is a borland thingy and it wont be compiled in other compilers. Use system("cls"); as it is compatible in most of the compilers.

btw clrscr(); / system("cls"); clears the whole previous outputs and it is kind of useful according to me.

system() should not be used... http://www.cplusplus.com/forum/articles/11153/
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]








Users browsing this thread: