Solved

stupid question about arrays

Posted on 2008-10-25
4
292 Views
Last Modified: 2010-04-15
say I have:

I am not really clear what int a[] means, does it mean that the function takes elements of an array or it takes an array ?

so say if I do:

func1(b, 'a');


or

func1(b[1], 'a');

which one is correct?
int a, b[1];
 

int func1 (int a[], char b) {

    return a[-1] + d[a[1] * 3];

    return a[1];

    return a[0];

}

Open in new window

0
Comment
Question by:kuntilanak
  • 2
4 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22804692
in C# lets say we have array called numbers

int[] numbers = {1, 2, 3, 4, 5};
//the first argument is passing complete array in simple words i.e as a reference
//2nd argument is just passing one element of array
            func1(numbers,numbers[0]);
        }
        public void func1(int[] a, int h)
        {
            int i = a[1];
            int j = a[2];
            int k = h;
        }
0
 
LVL 13

Accepted Solution

by:
josgood earned 500 total points
ID: 22804695
In your example, func1's argument a[] means that the function accepts an array of int;

The array contains elements, so saying "takes elements of an array" is the same as "takes an array", to my thinking.

In your example, the a[-1] is a real problem -- that's saying "the integer just before the first integer in a".  It is a complete crap shoot as to what value is in that location or whether the location is even a legal one to access.  It certainly is not legal to access it in this manner, although C will let you do it.

Here's an example where the output values are given next to the printf statements.
#include <stdlib.h>
int d[5] = { 10, 11, 12, 13, 14 };

int func0 (int a[], char b) {
    return a[0];
}

int func1 (int a[], char b) {
    return a[1];
}

int func2 (int a[], char b) {
    return a[1] + d[a[1] * 3];
}

int main(void) {
   int a[5] = { 0, 1, 2, 3, 4 };
   int a0 = func0(a,'q');
   int a1 = func1(a,'q');
   int a2 = func2(a,'q');
   printf("a0 = %d\n",a0); // 0
   printf("a1 = %d\n",a1); // 1
   printf("a2 = %d\n",a2); // 14
}
0
 
LVL 13

Expert Comment

by:josgood
ID: 22804698
Don't feel stupid for asking questions like this.

The only stupid action would be to know that you don't understand something and then be afraid to look stupid by asking it.

Every expert at this site has encountered things they didn't understand.  They became experts by getting answers to their question.

Well done for asking the question!
0
 

Author Comment

by:kuntilanak
ID: 22804737
thank you guys
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now