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
Solved

stupid question about arrays

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

829 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