Solved

stupid question about arrays

Posted on 2008-10-25
4
291 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

758 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

22 Experts available now in Live!

Get 1:1 Help Now