problem in adding array elements

J

JyotiR

Hi.
Pls help me i have the following code which is having error.This
program is being written by Visual studio 2005
Pls help me ASAP.
#include "stdafx.h"

void fun(int);
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int _tmain(int argc, _TCHAR* argv[])
{

fun(arr[0]);
return 0;
}
void fun(int *var)
{
int a = *(arr[0]+2);
int b = *(arr[0]+3);
int c;
c = a+b;
printf("\n %u",c);
}
Now the error is :
illegal indirection
 
K

Kai-Uwe Bux

JyotiR said:
Hi.
Pls help me i have the following code which is having error.This
program is being written by Visual studio 2005
Pls help me ASAP.
#include "stdafx.h"

void fun(int);
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int _tmain(int argc, _TCHAR* argv[])
{

fun(arr[0]);

Here, you pass an int to a function that expects an int*.

return 0;
}
void fun(int *var)
{
int a = *(arr[0]+2);

Here you dereference an int.

int b = *(arr[0]+3);

Same here.

int c;
c = a+b;
printf("\n %u",c);
}
Now the error is :
illegal indirection

What are you trying to do? Without knowing your goal, it is hard to suggest
how to fix the code.


Best

Kai-Uwe Bux
 
I

Ivan Vecerina

: Hi.
: Pls help me i have the following code which is having error.
: This program is being written by Visual studio 2005
: Pls help me ASAP.

The result of applying the [n] operator to an array is not
a pointer (int*), as your code seems to assume, but a
reference to the item (int&).
To obtain a pointer, you need to take the address of the
result (e.g. &arr[3]), or preferably simplify the expression
as illustrated below:

: #include "stdafx.h"
:
: void fun(int);
: int arr[10] = {1,2,3,4,5,6,7,8,9,10};
: int _tmain(int argc, _TCHAR* argv[])
: {
:
: fun(arr[0]);
-> fun( & arr[0] ); // explicitly take address of item 0
-> fun(arr); // same, but implicit converstion - preferred

: return 0;
: }
: void fun(int *var)
: {
: int a = *(arr[0]+2);
-> int a = *(&arr[0]+2); // ok, but way too complicated
-> int a = arr[2]; // simply, preferred option
-> int a = *(arr+2); // same as the above, but more verbose

hth -Ivan
 
A

Alf P. Steinbach

* JyotiR:
Hi.
Pls help me i have the following code which is having error.This
program is being written by Visual studio 2005
Pls help me ASAP.
OK.


#include "stdafx.h"

Non-standard header, just remove it.

Practical stuff: also remember to turn off "precompiled headers" in your Visual
Studio project settings.

void fun(int);

No need to forward-declare the function.

Just define it here.

It's the same with program text as with e.g. a technical book: always strive to
/define/ your terms (e.g. functions) before you use them.

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

Global variables are ungood.

Make this local in main.

int _tmain(int argc, _TCHAR* argv[])

Non-standard.

Here's the relevant valid standard main:

int main()


{

fun(arr[0]);
return 0;
}
void fun(int *var)

This function head doesn't match the earlier forward declaration.

{
int a = *(arr[0]+2);

arr[0] produces an integer, value of 1. Adding 2 yields 3. Then attempting to
apply * to that number is in error.

int b = *(arr[0]+3);
int c;
c = a+b;
printf("\n %u",c);
}
Now the error is :
illegal indirection

See above.


Cheers, & hth.,

- Alf
 
J

Jack.Thomson.v3

#include <stdio.h>

void fun(int);
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int main()
{

fun(arr[0]);
return 0;
}

void fun(int var)
{
int a = *(arr+2);
int b = *(arr+3);
int c;
c = a+b;
printf("\n %u",c);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top