2013-08-10 2 views
-2
  1. Я получаю предупреждение, как показано ниже: «передавая аргумент 1 из„arraySumLoc“от несовместимого типа указателя»
  2. В результате получаем через функцию arraySumLoc () не подходит, чтобы понять, почему.
  3. Если я хочу использовать maloc() во время объявления массива, какая будет модификация.

#include<stdio.h> 
#include<conio.h> 

typedef struct item { 
    double cost; 
    double commission; 
} item; 

typedef struct loc { 
    int x; 
    int y; 
} loc; 

double arraySumLoc(struct item *arr, loc fromLoc, loc toLoc) { 

    double sumOfCost = 0.0; 
    int fromX, fromY, toX, toY, indexX, indexY; 

    if (fromLoc.x > toLoc.x) { 
     fromX = toLoc.x; 
     toX = fromLoc.x; 
    } else { 
     fromX = fromLoc.x; 
     toX = toLoc.x; 
    } 

    if (fromLoc.y > toLoc.y) { 
     fromY = toLoc.y; 
     toY = fromLoc.y; 
    } else { 
     fromY = fromLoc.y; 
     toY = toLoc.y; 
    } 

    for(indexX = fromX; indexX <= toX; indexX++) { 
     for (indexY = fromY; indexY <= toY; indexY++) { 
      sumOfCost += ((struct item*)(arr+indexX))[indexY].cost; 
     } 
    } 
    return sumOfCost; 
} 

int main() { 

    int i, j, row, col; 

    printf("Enter number of row: "); 
    scanf("%d", &row); 
    printf("Enter number of Column: "); 
    scanf("%d", &col); 

    struct item product[row][col]; 

    for(i = 0; i < row; i++) { 
     for(j = 0; j < col; j++) { 
      scanf("%lf", &product[i][j].cost); 
     } 
    } 

    loc fromLoc, toLoc; 
    fromLoc.x = 0; 
    fromLoc.y = 0; 
    toLoc.x = row - 1; 
    toLoc.y = col - 1; 

    printf("\n\nSum of cost : %.2lf", arraySumLoc((struct item**)(&product), fromLoc, toLoc)); 
    getch(); 

    return 0; 
} 
+0

Добро пожаловать в переполнение стека! Не могли бы вы указать сообщение об ошибке? – Scientious

+1

@Scientious Sure: 'несовместимые типы указателей, проходящих 'struct item (*) [5] [10]' к параметру type 'struct item *'' –

+0

Также [прочитайте это] (http://c-faq.com/) aryptr/index.html). –

ответ

0

двойной arraySumLoc (структура пункт * обр, LOC fromLoc, LOC toLoc) {

double sumOfCost = 0.0;  
int fromX, fromY, toX, toY, indexX, indexY; 

if (fromLoc.x > toLoc.x) { 
    fromX = toLoc.x; 
    toX = fromLoc.x; 
} else { 
    fromX = fromLoc.x; 
    toX = toLoc.x; 
} 

if (fromLoc.y > toLoc.y) { 
    fromY = toLoc.y; 
    toY = fromLoc.y; 
} else { 
    fromY = fromLoc.y; 
    toY = toLoc.y; 
} 

for(indexX = fromX + 1; indexX <= toX; indexX++) { 
    for (indexY = fromY + 1; indexY <= toY; indexY++) { 
     sumOfCost += ((struct item*)(arr+indexX))[indexY].cost; 
    } 
} 
return sumOfCost; 

} Е ("\ п \ nSum из стоимости:% .2f" , arraySumLoc ((struct item **) (& продукт), fromLoc, toLoc));

+0

Спасибо за ваш очень быстрый ответ. 1. Я все еще получаю предупреждение, как показано ниже: «Передача аргумента 1 из« arraySumLoc »из несовместимого типа указателя« 2. Результат получается через функцию arraySumLoc() неправильно, неспособный выяснить почему. 3. Если я хочу использовать maloc() во время объявления массива, какая будет модификация. – Pothiq

-1

двойной arraySumLoc (структура пункт ** обр, LOC fromLoc, LOC toLoc) {

double sumOfCost = 0.0;  
int fromX, fromY, toX, toY, indexX, indexY; 

if (fromLoc.x > toLoc.x) { 
    fromX = toLoc.x; 
    toX = fromLoc.x; 
} else { 
    fromX = fromLoc.x; 
    toX = toLoc.x; 
} 

if (fromLoc.y > toLoc.y) { 
    fromY = toLoc.y; 
    toY = fromLoc.y; 
} else { 
    fromY = fromLoc.y; 
    toY = toLoc.y; 
} 

for(indexX = fromX + 1; indexX <= toX; indexX++) { 
    for (indexY = fromY + 1; indexY <= toY; indexY++) { 
     sumOfCost += (((struct item*)arr+indexX))[indexY].cost; 
    } 
} 
return sumOfCost; 

} Е ("\ п \ nSum из стоимости:% .2f", arraySumLoc ((структура пункт * *) (& товар), отLoc, toLoc));

+0

Неопределенное поведение за другим, downvoted. Еще хуже, чем исходный код. –

Смежные вопросы