English 中文(简体)
运行C方案时的分部分错(产出)和多层面阵列点
原标题:Segmentation fault when running C program (putchar() and multidimensional array pointers)
  • 时间:2012-04-28 18:03:32
  •  标签:
  • c
  • putchar

I m trying to run the following code on C:

#include "ex1_1.h"

void path(char **adj_mat, int u, int v)
{
        printf("test
");
        char temp = *adj_mat[1];
        putchar(temp);
}

int main()
{
    int u = 5;
    int v = 5;
    char mat[5][5]={
    { 0 ,  1 ,  1 ,  1 ,  0 },
    { 0 ,  0 ,  0 ,  0 ,  1 },
    { 0 ,  0 ,  0 ,  0 ,  0 },
    { 0 ,  0 ,  0 ,  0 ,  0 },
    { 0 ,  0 ,  0 ,  0 ,  0 }
    };
    char** adj_mat = (char**)&mat;
    printf("Mtest
");
    path(adj_mat, u, v);
    return 1;
}

和Im 带“ fault错”。

我不知道,为什么我猜测,它是如何用多维阵列点来使用沙子的。

感谢。

问题回答

www.un.org/spanish/ecosoc a <代码>char **。

宣布您的<代码>path功能为其中之一(即等值):

void path(char (*adj_mat)[5], int u, int v)
void path(char adj_mat[5][5], int u, int v)

并使用

path(mat, u, v);

最新信息:现在说明该指控的原因。

。 这意味着,<代码>马特/代码>可用作5-通知的参照点。 一般来说,<代码>T的narray将折成“代码>/code>的切身点。 (脚注T,not decay!)

请注意,在您通过<条码>/代码>时,您使用<条码>。 在C,不可能将一个阵列作为功能,但只能是“接收”点。

因此,char/code>的点到点与<代码>5-array>的点子/代码之间的区别是什么? 其类型。 他们的规模也很大。

附录一 页: 1

当你书写<代码>p[i]时,这等于*(p+i)。 这里有点算术。 <代码>p+i 具有价值(是一种压力)<代码>p+i*sizeof(char *),因为<代码>p各点至char*>。

查阅<代码>q时,q+i将具有q+i*sizeof(char[5]

这些变化几乎总是不同!


<>Update>/strong>(现在回答): 更糟的是,你是“伪造”的<代码>(*q)[5]“to be” a char p**(通过您的无效译本),因此,请你打电话。

char temp = *adj_mat[1];

adj_mat[1] will look at the 2nd row of your matrix an try to interpret its value as a pointer (but it is a 5-array!), so when you then dereference via *adj_mat[1] you will land somewhere in nirvana - segfault!

<代码>(char**)&mat是未经界定的行为。 <代码>&:> (换言之,指5个果园的阵列,你可以将其改为指点人。)

我将照发<条码>。

void path(char *arr,int u,int v) {
 char (*adj_mat)[v]=arr;
 ...
}

改变行文

char temp = *adj_mat[1];

页: 1

char temp = adj_mat[1];

问题是:

*adj_mat[1];

you try 页: 1 dereference a pointer 页: 1 an invalid address, namely the ASCII code of 1 (I suppose).





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签