Hi.. I want to get the memory addresses of an array.i'm having a compilation error with this code.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int isu[1000];
void dhanu(void *(isu+996), void *(isu+995));
int main(){

*(isu+998)=isu;
*(isu+997)=(isu+10);
 printf("%d\n",*(isu+998));
 printf("%d\n",isu);
 dhanu((isu+998),(isu+997));
}

void dhanu(void *(isu+996), void *(isu+995)){

 printf("%d \n",(int *)(isu+996));
 printf("%d \n",(int *)(isu+995));

error says:
error: In line 16 expected ‘)’ before ‘+’ token
error: In line 16 expected ‘;’, ‘,’ or ‘)’ before ‘void’

It works perfectly in this

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int isu[1000];
void dhanu(void *a, void *b);
int main(){
//isu=(int)(isu+998);
*(isu+998)=isu;
*(isu+997)=(isu+10);
 printf("%d\n",*(isu+998));
 printf("%d\n",isu);
 dhanu((isu+998),(isu+997));
}

void dhanu(void *a, void *b)){

 printf("%d \n",(int *)a);
 printf("%d \n",(int *)b);

So why can't we use void *(isu+996) instead of void *a !!

Dani AI

Generated

Short answer: you cannot use an expression like (isu+996) in a function parameter declaration. The parameter list must contain type specifiers and (optionally) identifiers — not arbitrary expressions. See the C declaration rules for details: C declarations.

The real problems in the posted code are both syntax and types. Using void *(isu+996) tries to put an expression where the compiler expects a declarator (hence the syntax error). Also storing pointer values into an int array (e.g. *(isu+998) = isu;) mixes pointers and integers and invokes undefined behavior; printing pointers with %d is likewise incorrect. A typical, correct approach is to use an array of pointer type (or separate data and pointer arrays) and pass the stored pointers to your function. For example:

int data[1000];
int *store[1000];

store[998] = &data[0];    // address of first element
store[997] = &data[10];   // address of 11th element

process(store[998], store[997]);

void process(int *p1, int *p2) {
    printf("%p\n", (void*)p1);
    printf("%p\n", (void*)p2);
}

Practical tips: do not try to assign to an array name (arrays are not assignable), use &array[i] or array + i to get element addresses, declare your storage with the correct pointer type, and print pointers with %p (cast to void *). Compile with -Wall -Wextra to catch type-mismatch warnings. This addresses the syntax error you saw and the deeper type-safety issues that hinted at.

Recommended Answers

All 3 Replies

this is the 16th line..

void dhanu(void *(isu+996), void *(isu+995)){

What exactly were you expecting to happen? What you're attempting is rather nonsensical.

All the variables & pointers must declare within this array.
I take the address of the 1st element & the 11th element and assign 2 pointers to it((isu+997),(isu+998)).Then pass those 2 address two the

void dhanu

method by using another 2 ponters.. in that methoda i'm having a link list to store those addresses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.