473,386 Members | 2,129 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,386 software developers and data experts.

Help for Lab mid-term

5
Hi guys.This week on friday, I'm gonna have the final exam for C lab.Most probably, the final will be about arrays and pointers.Therefore, i need a quality source to study those topics.If you know one, could you please let me know?And there is a question i would like to ask;

void f(int a)
{ a++;}
int main ()
{ int x[]={3,5,8};
f(x[1]);
printf ("%d",x[1]);


the answer is 5, however; i dont understand the point.If we change the code like ++a;
, we still get the same value.Why is this?
Dec 26 '07 #1
5 1529
weaknessforcats
9,208 Expert Mod 8TB
The argument of the function f is an int. That means the compiler makes a copy of the int used to call f. The ++a just changes the copy. The int in the calling function is not involved.
Dec 26 '07 #2
Laharl
849 Expert 512MB
The answer is the same because parameters in C are passed into the function and then a local copy is created. Unless you pass parameters as pointers or references, when the function ends, the local copies are destroyed and any changes to them are lost.
Dec 26 '07 #3
Kefuci
5
Yeah i get it know.You mean that to increase the array that i wrote i have to modify my function in terms of the array.Because the function depends on what type of variable we call.Please correct me if am wrong.Thank you by the way.
here is the programme:

Expand|Select|Wrap|Line Numbers
  1. void f(int a[], int n)
  2. {
  3.    int i;
  4.    for(i=0;i<n;i++)
  5.       a[i]++;
  6. }
  7.  
  8. int main()
  9. {
  10.    int x[]={1,3,8};
  11.    f(x,3);
  12.    printf("%d",x[1]);
  13.    return 0;
  14. }
Dec 26 '07 #4
manjuks
72
Instead pass address of x[1] to function f() and write a function f() to take pointer to int as parameter and increment it.
Dec 27 '07 #5
sicarie
4,677 Expert Mod 4TB
Kefuci-

You should also research "scope". Your code above should not work (and even if it does, if you make it any larger it probably won't) as your function f() is only modifying the local variable and returning nothing.
Dec 27 '07 #6

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

Similar topics

20
by: Joey Martin | last post by:
I am scanning an HTML file. I need to gather certain data from areas that start with <SMALL> text. Let me show the code, then explain more. ---- Set fso =...
9
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
0
by: hagar | last post by:
Hi all, I have a problem which I can not understand why this is happening! Debugging this I actually see that it grabs first record then when stepping through code to the line rsImportTo.AddNew...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
5
by: prkinlotpmpin | last post by:
I am in a begginers Visual Basic class and have to find out how to navigate through records. In my program, I can navigate to the "next record" but I have no idea how to write the code to go to the...
3
by: satan | last post by:
I'm having problem to test a recursive version of a binary search algorithm. These are my codes: public class OrderedArrayList extends ArrayListClass { //default constructor public...
1
by: murdla | last post by:
Hello. I am working on a project where users can automatically create personnel advices on the mainframe through a .NET Web Service. My current problem is that I am trying to call a...
2
by: mqueene7 | last post by:
below is my code for my merge sort but I can't get it to sort properly. I am trying generate random numbers based on input from the user and then sort those random numbers. Can you tell me what I...
7
by: pooh1119 | last post by:
hi. my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but...
6
by: natvarara | last post by:
#include <stdio.h> #include <string.h> #include <conio.h> #include <ctype.h> #define MAX 30 void getData (FILE*,int,char,int,int,float,float,int,int*); float computeTotal...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.