?
Solved

stupid question about arrays

Posted on 2008-10-25
4
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.
Suggested Courses

770 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