我正在尝试从遗留代码中获取服务并在injector()返回undefined时遇到奇怪的错误:
非常感谢,非常感谢任何指针或建议.
例如:如果用户输入是11234517并且想要在此输入中看到1的数量,则输出将是"1的数量是3.我希望您理解我的意思.
我只能计算整数中的位数.
#include <stdio.h>
int main()
{
int n, count = 0;
printf("Enter an integer number:");
scanf("%d",&n);
while (n != 0)
{
n/=10;
count++;
}
printf("Digits in your number: %d",count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
也许阵列是解决方案.任何帮助,将不胜感激.谢谢!
我无法弄清
我的Valgrind给出的
代码中的内存泄漏
==26373== HEAP SUMMARY:
==26373== in use at exit: 24 bytes in 1 blocks
==26373== total heap usage: 7 allocs, 6 frees, 136 bytes allocated
==26373==
==26373== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
==26373== at 0x4C2ABBD: malloc (vg_replace_malloc.c:296)
==26373== by 0x400592: graph_init (in /media/goutam/CommonPartition/My Research/DataStructures/Graphs/main)
==26373== by 0x40081D: main (in /media/goutam/CommonPartition/My Research/DataStructures/Graphs/main)
Run Code Online (Sandbox Code Playgroud)
这是创建内存泄漏的代码
/*
* Allocates memory for graph,adjacency matrix and nodes
*/
graph_t* graph_init(int n){
graph_t *graph = malloc(sizeof(graph_t)); …Run Code Online (Sandbox Code Playgroud) 谁能帮我?两者之间有什么区别吗?
if (!n / 10)
return;
Run Code Online (Sandbox Code Playgroud)
和
if (n / 10 == 0)
return;
Run Code Online (Sandbox Code Playgroud) 好的,这是我在Ubuntu 17.10上使用的初始代码.
#include <unistd.h>
#include <stdio.h>
void main()
{
printf("Demonstrating fork():\n");
fork();
printf("After fork():\nProcess Id is %d\n", getpid());
}
Run Code Online (Sandbox Code Playgroud)
所以是的,我执行了两次确认.但后来我注意到在线GCC编译器和我大学校园里的RedHat,输出结果非常不同:
现在据我所知,fork()创建了调用它的进程的另一个实例.但是在Ubuntu中,它似乎是从代码中引入它的位置复制的,而不是整个Process.为了检测它,我稍微查了一下代码:
#include <unistd.h>
#include <stdio.h>
void main()
{
fork();
printf("Demonstrating fork():\n");
printf("After fork():\nProcess Id is %d\n", getpid());
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,我对输出的观察是正确的:
所以任何人都可以证实这一点,并解释为什么会发生这种情况,或者只是我系统上的一个错误?
我目前正在开发一个程序,如果设置了环境变量,我想打印特殊输出。
例如,假设我想要环境变量"DEBUG"。
在我的 bash 命令提示符中,我DEBUG通过键入以下命令进行设置:
DEBUG=
然后在我的 C 程序中,我可以通过打印出char **environ. DEBUG确实出现在此环境打印输出中。
但是,我不知道如何检索此环境变量以进行条件检查。我试过像这样使用 getenv 函数:
getenv("DEBUG")
Run Code Online (Sandbox Code Playgroud)
如果我尝试打印出如下所示的输出,则会出现段错误:
printf("get env: %s\n", getenv("DEBUG"));
Run Code Online (Sandbox Code Playgroud)
我什至在一个已知的环境变量上试过这个,比如"HOME":
printf("get env: %s\n", getenv("HOME"));
Run Code Online (Sandbox Code Playgroud)
这仍然会产生段错误。
有没有人有任何经验检查是否从 C 程序设置了环境变量?我什至在拉出一个阻止我这样做的环境变量时也遇到了问题。
我试图在一些公开的线性代数代码中使交流功能起作用.
公开的原型是......
int ilaenv_(int *, char *, char *, int *, int *,int *, int *);
Run Code Online (Sandbox Code Playgroud)
公开的代码有函数调用...
nb = ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
Run Code Online (Sandbox Code Playgroud)
其中m,n,c_1和c_n1是整数,
错误消息是.
C++ 11 does not allow conversation from string literal to char *.
Run Code Online (Sandbox Code Playgroud)
我没有创建代码,但是从LAPACK站点下载了代码.我不愿意对可能有效的公开代码进行太多更改,以免引入错误.但是,此错误出现在我正在处理的程序中的许多函数中.
我该如何解决这个问题?
我用C编写了以下'Hangman game'程序(编写得很快,很脏,所以不要太在意代码):
#include <stdio.h>
int main() {
char word[] = "cat";
int amountGuessesAllowed = 5;
size_t size = sizeof(word)-1; // -1 to exclude the '\0'
char guessedWord[size];
for (int i=0; i<size; i++){
guessedWord[i] = '_';
}
int lettersGuessed = 0;
int totalTimesGuessed = 0;
char c;
printf("%s\n", guessedWord);
printf("Guess %d - Enter a character: ", totalTimesGuessed+1);
scanf("%s", &c);
while (totalTimesGuessed < amountGuessesAllowed) {
for(int i=0; i<size; i++){
if (word[i] == c && guessedWord[i] == '_'){
printf("It matches character at index …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
FILE *openfile()
{
FILE *fp1;
fp1 = fopen("test.c","r");
return fp1;
}
int main()
{
char c;
FILE *fp;
int a = 10;
int b = 1000 - a;
printf("Hello\n");
fp = openfile();
fscanf(fp, "%c", &c);
fclose(fp);
printf("%c", c);
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我在一个函数中打开一个文件描述符并传递给其他函数.这工作正常.
fp1在函数openfile()的本地声明.因为我们传递地址,从主函数我能够使用文件指针结构.
我的问题是FILE结构存储在哪里?结构的范围是什么?
编译器可以选择在内存中使用单个副本表示所有相同的字符串文字.例如 :
/*Case 1*/
char *s1 = "foo";
char *s2 = "foo";
printf("s1 points to : %p\n", s1);
printf("s2 points to : %p\n", s2);
Run Code Online (Sandbox Code Playgroud)
给
s1 points to : 0x40063c
s2 points to : 0x40063c
Run Code Online (Sandbox Code Playgroud)
然而,
/*Case 2*/
char *s1 = "foo";
char *s2 = (char[]){'f', 'o', 'o', '\0'};
printf("s1 points to : %p\n", s1);
printf("s2 points to : %p\n", s2);
Run Code Online (Sandbox Code Playgroud)
给
s1 points to : 0x40070c
s2 points to : 0x7ffe2866fcd0
Run Code Online (Sandbox Code Playgroud)
这是完全可以的,因为s1指向一些不变的东西,而s2则没有.
但是,有没有办法告诉编译器,在case2中,我们实际上希望将s2指向某些常量数据 - 但保持其声明样式 - 以便如果s2指向的数据与s1指向的数据相同,编译器可以在存储器中继续进行单个数据副本.
我试过了
const …Run Code Online (Sandbox Code Playgroud)