Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 621 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


if you faced any problem feel free to ask
Reply

#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)

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

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)

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

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?

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Reply

#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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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

#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.
Reply

#5
Hello Everyone,
Really good information and conversation regarding Array function in C language,
Thanks all of you for sharing information.
Reply

#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.
Reply

#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.
Reply

#8
Quote:(01-12-2013, 07:48 AM)chemicalrage Wrote:

[To see links please register here]

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...

[To see links please register here]

Reply

#9
Quote:(01-12-2013, 07:48 AM)chemicalrage Wrote:

[To see links please register here]

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...

[To see links please register here]

Reply

#10
Quote:(01-12-2013, 07:48 AM)chemicalrage Wrote:

[To see links please register here]

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...

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through