Tag: 指针

C系统呼叫和消息

我试图通过传递带有该调用的消息来传递指向系统调用的指针。 指针应该是int []。 但是,消息定义需要传递char指针。 如何发送int指针? int pidarray[j]; m.m1_p1 = pidarray; 是否可以转换指针类型?

来自GCC编译器的程序段错误:连接两个字符串

以下代码从GCC编译器引发segment error : char *str1 = “India”; char *str2 = “BIX”; char *str3; str3 = strcat(str1, str2); printf(“%s %s\n”, str3, str1); 问题是str3=strcat(str1, str2) 。 所以我的问题是:对于GCC编译器,它禁止指向同一个东西的两个指针?

将int转换为int指针地址

C ++ p指向特定的地方 int * p 当我尝试p=p[1]它表示无法将int转换为int(使用devcpp)。 虽然p=&p[1]工作正常 为什么我需要做第二种方法? p[1]是一个地址。 那么第一种方法应该有效吗? 你能解释一下这个错误吗?

在c 中使用指针编写合并排序的问题

我正在尝试用指针编写一个合并排序程序,它接近工作正常,但是存在的问题是在输出中有一些’0’而不是一些排序数组的数字。 为了测试代码,你必须编写一个txt文件prova.txt ,其中有数组,一个数字用于行。 例如: prova.txt: 4 7 2 9 1 45 87 运行代码时,我期待输出 0: 1 1: 2 2: 4 3: 7 4: 9 5: 45 6: 87 但我明白了 0: 1 1: 0 2: 0 3: 0 4: 0 5: 0 6: 2 此外,有什么建议可以让我改进我的代码吗? #include int *merge(int left[], int right[], int n){ int *ordinato, i=0, j=0; […]

用指针在C中排序结构

我刚刚从C开始,对幕后发生的事情一无所知。 我正在为数据结构类动态学习它,这使得事情变得更加艰难。 更新:我已经取消了该程序,并开始使用内存并启动。 我在那里有allocate和deallocate函数,我得到一个malloc错误:Q1(9882)malloc: *对象0x7fff59daec08的错误:没有分配被释放的指针*在malloc_error_break中设置一个断点来调试 Update2这里是我的修改后的代码,它仍然缺少一些东西,我的几个printf语句没有出现: #include #include #include #include static int size = 10; struct student{ int id; int score; }; struct student* allocate(){ /*Allocate memory for ten students*/ struct student *s = malloc(size*(sizeof(struct student))); assert(s != 0); /*return the pointer*/ return s; } void generate(struct student* students){ /*Generate random ID and scores for […]

C编程错误,打印链表,在运行时执行代码崩溃

我正在研究链表和指针。 这是一个包含推送function的简单代码。 在推送我的元素并尝试打印第一个成员后,执行的代码在运行时崩溃。 但是,当将相同的指针传递给print_list函数并在print_list中应用printf函数时,它可以正常工作。 但是当在main函数中直接使用它并应用printf函数时,它会崩溃。 #include #include typedef struct list{ int order; struct list *next; }list; void push(struct list **arg,int i); int main() { struct list **ptr=NULL; for(int i=0;iorder); //Here run time error return 0; } void push(struct list **arg,int i){ struct list *temp; temp= malloc(sizeof(list)); temp->order=i; temp->next=*arg; *arg=temp; } void print_list(list ** head) { […]

类型限定符对存储位置的影响

正如标题中所提到的,如果type-qualifiers影响声明bss的存储位置( stack , bss等),我很困惑。为了描述更多,我正在考虑以下声明。 int main() { const int value=5; const char *str= “Constant String”; } 在上面的代码中,默认的storage-class-specifier是auto 。 因此,假设这些常量将在创建时在main的stack-frame中分配。 通常, pointers堆栈中各种存储器位置的pointers可以自由地修改其中包含的值。 因此,从以上几点可以理解, type-qualifier添加了一些逻辑来保存所存储元素的constant性质(如果是这样的话是什么?)或者constants存储在存储器的read-only-portion中。 请详细说明 。 更详细的例子 #include int main(void) { int val=5; int *ptr=&val; const int *cptr=ptr; *ptr=10; //Allowed //*cptr=10; Not allowed //Both ptr and cptr are pointing to same locations. But why the following […]

如果我的计算机是32位系统,它有32位地址吗? 但是当我在C中打印任何内存地址时,为什么我的地址<32bit?

例如 printf(“%u”,&a); 给我输出 65524 这是一个16位地址。

指针C中的N链表

我写了下面的代码不起作用。 应用程序在打印方法上崩溃。 知道哪里出错了吗? #include #include typedef struct { int item; struct LinkedList *Next; } LinkedList; int main() { LinkedList vaz; vaz.item = 14123; vaz.Next = 0; add(&vaz,123); add(&vaz,9223); Print(&vaz); return 0; } void Print(LinkedList* Val) { if(Val != 0){ printf(“%i\n”,(*Val).item); Print((*Val).Next); } } void add(LinkedList *Head, int value) { if((*Head).Next == 0 ) { LinkedList […]

C – 使用execvp和用户输入

我正在尝试让我的C程序从用户那里读取Unix参数。 到目前为止,我一直在搜索这个网站,但是我还没弄清楚到底我做错了什么 – 尽管我的指针实现技能相当有限。 以下是我现在的代码方式; 我一直在乱搞指针而没有运气。 错误也说我需要使用const * char,但我在其他例子中看到* char可以由用户输入。 #include #include #include #include main() { char args[128]; //User input printf(“> “); fgets(args, 128, stdin); execvp(args[0], *args[0]); } 我得到的错误如下: smallshellfile.c: In function ‘main’: smallshellfile.c:13:21: error: invalid type argument of unary ‘*’ (have ‘int’) smallshellfile.c:13:5: warning: passing argument 1 of ‘execvp’ makes pointer from integer without […]