有人可以帮我解决如何在这里释放二维数组.我尝试使用循环释放但我得到一个错误说:
*** glibc detected *** ./assignment4: free(): invalid pointer: 0x0932b196 ***
Run Code Online (Sandbox Code Playgroud)
问题是当我单独释放第0个元素时,我没有遇到任何问题.但是当我释放元素[1]到[7]时,我得到了错误.
char ** stringOne;
stringOne = malloc(sizeof(char*)*8);
stringOne[0] = malloc(sizeof(char)*6);
stringOne[0] = strtok(listofdetails[0], " ");
for(i=1;i<8;i++)
{
stringOne[i] = malloc(sizeof(char)*6);
stringOne[i] = strtok(NULL, " ");
}
for(i=0;i<8;i++) // FREEING memory
{
free(stringOne[i]);
}
Run Code Online (Sandbox Code Playgroud) 我使用Visual Studio 2013与标准C.
我的程序需要四行才能
{"Analysis 1", "AN 1", 0, 0.0667}输入Fach[0].
我不想浪费这么多行来填充这个数组.
有没有更好的方法将这四行合二为一?
#define _CRT_SECURE_NO_WARNINGS
#include "header.h"
#include <stdio.h>
#include <string.h>
struct sFach
{
char Mod[40]; //Modulbezeichnung
char Ab[5]; //Abkürtzung
int Note; //Note
double Gew; //Gewichtungsfaktor
};
int main()
{
struct sFach Fach[31];
strcpy(Fach[0].Mod, "Analysis 1");
strcpy(Fach[0].Ab, "AN 1");
Fach[0].Note = 0;
Fach[0].Gew = 0.0667;
strcpy(Fach[1].Mod, "Algebra");
strcpy(Fach[1].Ab, "AL");
Fach[1].Note = 0;
Fach[1].Gew = 0.0889;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要修改现有函数,有一些const输入参数:
int f(const owntype *r1, const owntype *r2)
Run Code Online (Sandbox Code Playgroud)
为了做到这一点,我想调用一个使用相同类型但没有const关键字的子函数:
void subfunction (owntype *src, owntype *dst)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了这个(以及其他一些变体),但它不起作用:
int f(const owntype *r1, const owntype *r2) {
...
subfunction((const owntyp*) r1);
...
}
Run Code Online (Sandbox Code Playgroud)
如何在不需要更改两个函数的parmeter描述的情况下编译它?
我无法理解以下代码如何给出不同的输出
#include <stdio.h>
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 4321
printf("%d\n",printf("%d",printf("%d ",i)));
Run Code Online (Sandbox Code Playgroud)
输出: 43 31
printf("%d\n",printf("%d ",printf("%d ",i)));
Run Code Online (Sandbox Code Playgroud)
输出: 43 3 2
printf("%d\n",printf("%d ",printf(" %d ",i)));
Run Code Online (Sandbox Code Playgroud)
产量 43 4 2
printf("%d\n",printf(" %d ",printf(" %d ",i)));
Run Code Online (Sandbox Code Playgroud)
输出: 43 4 3
和其他变化也给出了其他产出.
如何只是一个空格改变一个数字.
提前致谢.
int main(){
int *p=new int[5];
//case 1: delete p;
//case 2: p++;delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我分别使用案例1和案例2会发生什么?
这是我的 makefile。我知道有一种更短的方法可以写出来,但我有一个关于如何运行它的问题。我的硬件说我必须使用命令: make run --- 该命令应该导致可执行文件使用文件重定向来运行以读取输入文件数据。
我该如何设置呢?
我也知道 gcc 应该是选项卡式的。
test: main.o sum.o stack.o bSearch.o database.o db.o set.o parse.o bubble.o
gcc -o object main.o sum.o stack.o bSearch.o db.o set.o parse.o bubble.o
main.o: main.c sum.h
gcc -c main.c
sum.o: sum.c sum.h
gcc -c sum.c
stack.o: stack.c stack.h
gcc -c stack.c
bSearch.o: bSearch.c defs.h sortAndsearch.h
gcc -c bSearch.c
database.o: database.c defs.h parse.h
gcc -c database.c
db.o: db.c defs.h
gcc -c db.c
set.o: set.c set.h db.h
gcc -c set.c
parse.o: parse.c parse.h
gcc …Run Code Online (Sandbox Code Playgroud) 我观察到int *x = malloc(sizeof(int));这个代码正在尝试将void*转换为int*而不使用正确的类型转换.所以,根据我的答案应该是选择一个.但是在官方的GATE-2017考试答案中,答案是给予D.所以我错了吗?怎么样 ?
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int *assignval(int *x, int val){
*x = val;
return x;
}
void main(){
clrscr();
int *x = malloc(sizeof(int));
if(NULL==x) return;
x = assignval(x,0);
if(x){
x = (int *)malloc(sizeof(int));
if(NULL==x) return;
x = assignval(x,10);
}
printf("%d\n",*x);
free(x);
getch();
}
Run Code Online (Sandbox Code Playgroud)
在我看来,选项D只在
int *x = (int *)malloc(sizeof(int));使用时才是正确的.
我尝试了很多次来修复错误” Incomplete Definition of Type of struct *,请您能帮我找到问题并解决吗?
有什么建议吗?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct mypoint;
typedef struct mypoint {
int x;
int y;
};
void setRandomPoint(struct mypoint *a, int min, int max)
{
do {
a->x = rand();
} while (a->x > max || a->x < min);
do
{
a->y = rand();
} while (a->y > max || a->y < min);
}
int main()
{
struct myPoint point;
int min = 0, max = 0;
int i;
srand(time(NULL));
while …Run Code Online (Sandbox Code Playgroud) int main(int argc, char *argv[])
{
struct sockaddr_in src = { .sin_family=AF_INET, .sin_addr.s_addr=INADDR_ANY, .sin_port=htons(90) };
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
bind(fd, (struct sockaddr *)&src, sizeof(src));
char buf[1024];
ssize_t res = recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
printf("res=%zi\n", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并执行此程序之后。在另一个终端上执行
nc -u localhost 90
Run Code Online (Sandbox Code Playgroud)
测试自从我使用INADDR_ANY以来,我是否确实从“任何接口”接收到一些udp流量。但是程序只是挂起。我想念什么?