#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *forename;
char *surname;
int age;
};
void change_struct(struct Person *person, char *forename, char *surname,
int age);
void print_struct(struct Person *person);
int main(void)
{
struct Person person1;
person1.forename = malloc((strlen("Max") + 1) * sizeof(char));
if (!person1.forename) {
exit(EXIT_FAILURE);
}
strcpy(person1.forename, "Max");
person1.surname = malloc((strlen("Mustermann") + 1) * sizeof(char));
if (!person1.surname) {
exit(EXIT_FAILURE);
}
strcpy(person1.surname, "Mustermann");
person1.age = 35;
print_struct(&person1);
change_struct(&person1, "Hans", "Bauer", 45);
print_struct(&person1);
free(person1.forename);
free(person1.surname);
exit(EXIT_SUCCESS);
}
void change_struct(struct Person …Run Code Online (Sandbox Code Playgroud) 我会非常简短!这是一个可重复的例子:
Z1 <- matrix(rep(c(1, 0, 0, 0, 0,
1, 1, 0, 0, 0,
1, 0, 1, 0, 0,
1, 0, 0, 1, 0,
1, 0, 0, 0, 1),
times = 3),
nrow = 15,
ncol = 5,
byrow = TRUE)
z <- matrix(c(0.1, 0.2, 0.3, 0.4, 0.5,
0.6, 0.7, 0.8, 0.9, 1,
1, 2, 3, 4, 5),
nrow = 3,
ncol = 5,
byrow = TRUE)
Run Code Online (Sandbox Code Playgroud)
我需要一个可扩展的非for循环解决方案(例如,涉及来自apply-family的东西),它给出了我现在将手动执行的这些计算的等效结果:
Zz1 <- Z1[1:5,] %*% z[1,]
Zz2 <- Z1[6:10,] %*% …Run Code Online (Sandbox Code Playgroud) 这与我之前的一个问题有关.我尝试了一点点扩展代码,并尝试使用不同的方法返回指向本地静态变量的指针,尤其是返回二维数组.(这只是为了理解数组的指针是如何工作的以及它们在函数环境中的行为方式.它甚至没有使用structs 以智能方式返回数组的最轻微的意图.)我编写了两个转置矩阵的函数.该函数transpose()仅转换2x4矩阵,该函数transpose_generic()应该转置任意大小的矩阵.他们没有真正的用处.在我声明并定义了我称之为的函数之后int main(int argc, char *argv[]){}.已经转置的两个矩阵应该以stdout简单的for循环打印.这是我不明白的事情.根据我是否声明应该转换为全局变量或局部变量的矩阵,我得到段错误或"干净"退出.我还可以通过在打印转置矩阵的for循环之后使用for循环来阻止编译程序的segfaulting stdout.但for循环需要使用新索引.我敢肯定我错过了一些非常基本的东西,但我无法自己解决这个问题.即使经过广泛的互联网搜索,我仍然感到困惑.所以也许有人可以开导我.这是代码:
#include <stdio.h>
int mat1[2][4] = {
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int mat2[2][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8}
};
typedef matx[2];
matx *transpose(int matrix[][4]);
typedef maty[];
maty *transpose_generic(int nrow, int ncol, int matrix1[nrow][ncol], int matrix2[nrow][ncol]);
int main(int argc, char *argv[]) {
/* This may need a little bit …Run Code Online (Sandbox Code Playgroud) 假设我声明了以下变量:
int num;
num = 0;
int main(void)
{
/* ... */
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
编译器会抱怨num未声明,并且默认输入int.当我一步到位时,这不会发生:
int num = 0;
Run Code Online (Sandbox Code Playgroud)
或者如果我将作业转移到main():
int main(void)
{
num = 0;
/* ... */
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
我曾经读过这种行为的解释,但我再也找不到了.有人可以再次更新我.
我正在编译
gcc -std=c11 -O2 -g -pedantic -Wall -c -fmessage-length=0 -v
Run Code Online (Sandbox Code Playgroud)