我将值分配给状态数组,如下所示:
状态[i] += 1;
我喜欢从 fortran
访问这个数组我怎样才能访问这个数组?
例如,我想从 fortran 中更改 STAT 的值,如下所示:
统计(2)=3
这可能吗?
c源
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
void call_fc_ (int *key, int *addr, int *size, int *status)
{
int i;
int shmid;
void* shared_addr;
//printf("first ptr = %p\n", *addr);
shmid = shmget (*key, *size, IPC_CREAT | IPC_EXCL | 0666);
if (shmid == -1)
{
printf("shmget is failed!\n");
exit(0);
}
shared_addr = (void*) shmat(shmid, 0, 0);
status = (int*)shared_addr;
//printf("status ptr = %p\n", status);
int data_size = *size/sizeof(int);
for(i=0; i<data_size;i++) {
status[i] += 1;
printf("%d th value : %d \n", i, status[i]);
}
}
源代码
IMPLICIT NONE
INTEGER*8 KEY,SIZE,ADDR
DATA KEY / 777 /
DATA SIZE / 64 /
!DATA ADDR / Z'b76fb000' /
CALL CALL_FC(KEY, ADDR, SIZE, STAT)
PRINT *, 'stat is : ', STAT
! CAN I ACCESS TO STAT LIKE THIS?
!DO I=1,10
!STAT(I) = STAT(I) + 5
!WRITE (*,*) STAT(I)
!END DO
我已经测试过这段代码,我参考了这个问题的好答案。但是当我尝试这样做时出现分段错误错误:
integer(c_int) :: key = 777, ssize = 64, addr
integer, pointer, dimension(:) :: stat
type(c_ptr) :: statptr
!DATA KEY / 777 /
!DATA SIZE / 64 /
print *, 'before stat size = ', size(stat)
call call_fc(key, addr, ssize, statptr)
!print *, 'statptr = ', statptr
call c_f_pointer(statptr, stat, [ssize])
print *, 'after stat size = ', size(stat)
stat(1) = 111 <==
stat(2) = 222
stat(3) = 333
print *, 'stat : ', stat
你能认出这是怎么回事吗?