我一直在编写我的大学,我们正在处理矩阵,我找不到代码中的错误,该错误正在改变我保存矩阵列的变量值。我已经尝试过调试它并且找不到它,它只是结束了我在矩阵中分配内存的函数,并输入了下一个函数(从键盘获取值来填充矩阵)的错误列值。 代码是下一个:
#include <stdio.h>
#include <stdlib.h>
#define DEBUG 1
void allocate (int ***mat,int n,int m){
int i;
*mat = (int **) malloc (n*sizeof(int*));
for (i=0; i<n; i++){
mat[i] = (int *) malloc (m*sizeof(int));
}
#if DEBUG
printf ("allocate n: %d m: %d\n",n,m);
#endif // DEBUG
}
void initialize (int **mat, int n, int m){
int i,j;
#if DEBUG
printf ("initialize n: %d m: %d\n",n,m);
#endif // DEBUG
for (i=0; i<n; i++){
for (j=0; j<m; j++){
printf ("Enter value for position [%d][%d]: ",i,j);
scanf ("%d",&(mat[i][j]));
}
}
}
int main()
{
int n=2;
int m=3;
int **mat=NULL;
#if DEBUG
printf ("before allocate n: %d m: %d\n",n,m);
#endif // DEBUG
allocate (&mat,n,m);
#if DEBUG
printf ("after allocate n: %d m: %d\n",n,m);
#endif // DEBUG
initialize (mat,n,m);
return 0;
}
因此,如果在DEBUG设置为1的情况下运行此操作,您将获得n和m的值(这是我的行和列)。我正在使用代码块。 谢谢你的时间!
答案 0 :(得分:1)
更新功能
---
name: home
url: /
---
<div ng-controller="MainCtrl">
<header>
<p class="sponsored" id="top">Sponsored by </p>
<img src="http://placehold.it/200x30" class="sponsors" alt="">
<h1>Business Directory</h1>
<div class="find">
<input type="search" placeholder="What are you looking for?" ng-model="query">
</div><!-- /.find -->
</header>
<div class="businesses">
<div class="storeIcon">
<img src="/assets/img/store.png" class="store" alt="">
</div><!-- /.storeIcon -->
<p class="number">Search {{businesses.length}} businesses in Brandon</p><button class="filter button">Filter by <i class="fa fa-chevron-down"></i></button>
<div class="options">
<div class="cat">
<div class="categories">
<div class="group">
<p class="name">Grade Level</p>
<div class="check">
<input type="radio" name=""><p>Auto</p>
<input type="checkbox" name=""><p>Restaurant</p>
<input type="checkbox" name=""><p>Other</p>
</div><!-- /.check -->
</div><!-- /.group -->
<div class="group">
<p class="name">School Type</p>
<div class="check">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div><!-- /.check -->
</div><!-- /.group -->
</div><!-- /.categories -->
</div><!-- /.cat -->
</div><!-- /.options -->
</div><!-- /.businesses -->
<div class="all">
<div class="business large-4.columns" data-ng-repeat="business in businesses | filter:query | orderBy:'name'" >
<div class="overlay">
<a href=""><img src="http://placehold.it/300x300" class="storefront" alt=""></a>
</div><!-- /.overlay -->
<div class="info">
<a href=""><p class="name">{{business.name}}</p></a>
<p class="description">{{business.description}}</p>
<p class="address">{{business.address}}</p>
<a href="" class="website">{{business.website}}</a>
</div><!-- /.info -->
</div>
</div>
<footer>
<hr>
<i class="fa fa-twitter"></i>
<i class="fa fa-facebook"></i>
<div class="backContainer">
<a href="#top"><p class="back">Back to top</p></a>
</div><!-- /.backContainer -->
</footer>
</div>
答案 1 :(得分:1)
http://coliru.stacked-crooked.com/a/4d3cb5ed16ae73a5
// add item to cart on visit depending on cart total value
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( isset($_POST['mix']) && !empty($_POST['mix']) && isset($_POST['mix_name']) && !empty($_POST['mix_name']) ) {
global $woocommerce;
$addcart = $woocommerce->cart->add_to_cart( $product_ID = 700, $quantity=1, $variation_id= '', array('attribute_strain' => 'sativa', 'attribute_level' => 'cbd', 'attribute_flavor' => 'flower shop') );
foreach ($woocommerce->cart->cart_contents as $cart_item_key => $values) {
$_product = $values['data'];
//find all simple product type to remove
if($_product->product_type == 'simple') {
unset($woocommerce->cart->cart_contents[$cart_item_key]);
}
}
if($addcart) {
wp_safe_redirect( WC()->cart->get_checkout_url() );
exit;
} else {
wc_print_notices();
_e('Can\'t add');
}
}
}
您看,实际上您没有使用void allocate (int ***mat,int n,int m){
int i;
*mat = (int **) malloc (n*sizeof(int*));
for (i=0; i<n; i++){
//This is where the error is.
(*mat)[i] = (int *) malloc (m*sizeof(int));
}
#if DEBUG
printf ("allocate n: %d m: %d\n",n,m);
#endif // DEBUG
}
引用数组中的特定单元格。不,您实际引用了指向矩阵的指针,然后将其索引到列或行,这意味着您为mat[i]
分配了内存,而不是int*
。
因此,您需要将原始矩阵指针与矩阵相关联,然后索引 - &gt; int
。