Solved

stupid question about arrays

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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