954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pointer and array arithmetic

question about pointer and array.
can we add a 2-D array (**A) with a 1-D array (*B)? both of the array is define as a pointer in the program.
where array A is A[i][j] and B is B[i]

new in C programming and pointer is difficult.... :-|

yaya
Newbie Poster
4 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Um... yes. But not with the + operator, of course; with some specially built procedure. I don't know what you mean by 'add', either, but surely there's some way for you to write the algorithm.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

the solution i need to obtain is
sum[i] = A[i][j] + B[i];

couldn't add 2-d array and 1-d array using + or using IMSL libraries. is it cause of A is a 2-D array? is that mean that sum[i] and B[i] have to be 2-D array?

i'm try to use this method (something similiar), where *B will become **B,

void pointTo(char **src, char *dst);
int main() {
/* local variables */
char text[3];
char *ptr;
/* point our pointer to our local variable */
pointTo(&ptr, text);
/* modify pointer data; affects local data due to pointer linkage */
ptr[0] = 'A';
ptr[1] = '\0';
/* end program */
return 0;
}
void pointTo(char **src, char *dst) {
*src = dst;
}

yaya
Newbie Poster
4 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>the solution i need to obtain is
>sum[i] = A[i][j] + B[i];
What are the types of A, B, and sum? I really don't see what the problem is unless A[i][j] and B[i] are incompatible for addition, or sum[i] can't hold a value of the result.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>the solution i need to obtain is >sum[i] = A[i][j] + B[i]; What are the types of A, B, and sum? I really don't see what the problem is unless A[i][j] and B[i] are incompatible for addition, or sum[i] can't hold a value of the result.

the actual function i'm trying to get is

for (j=0; j < N_tm[1]; j++){
for (i=0; i < N_sp[1]; i++)
{
*P = *R + gm[i][j];
*P++;
}
}

previously instead of gm[][], i had used **gm. but this still doesn't work.

yaya
Newbie Poster
4 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>the actual function i'm trying to get is
That's not helpful and you're not listening to my question. Post the declarations of the relevant variables.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

all variables are float.

the solution couldn't be calculate using pointer like that one i post previously.

solution should be used is
P[i] = R[i] + gm[i][j]

this is only working with array, not pointer.

yaya
Newbie Poster
4 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

all variables are float.

the solution couldn't be calculate using pointer like that one i post previously.

solution should be used is P[i] = R[i] + gm[i][j]

this is only working with array, not pointer.


Maybe someone else is interested in divining your problem from the babble you've given us, but I'm not. Perhaps when you can mange to describe your problem in a useful way and give us enough informatin to help you, I'll lend my assistence.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

plz help to write a c program to add to matrix using pointer.

somnath Basak
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You