[英]the C program does not execute the function outside the main
如果我在另一個C程序中將exec()函數作為主要函數執行,則它運行良好,而如果我將它作為在主菜單中調用的函數放置,則會給我一些警告,並且該函數無法運行。
我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int exec (void) {
char array[100];
char character;
int i = 0;
char* point;
int j = 0;
printf ("Digita una stringa");
printf ("\n");
do {
character = getchar();
array[i] = character;
i++;
}
while (character != '\n');
array[i-1] = '\0';
i = 0;
char* string[100];
char *word = strtok(array, " .");
j = 0;
while (word != NULL) {
printf("%s\n", word);
string[j++] = word; // Increment j
word = strtok(NULL, " .");
}
string[j] = NULL; // Make sure the array is NULL term
printf ("\n");
pid_t pid;
pid = fork();
int status;
if (pid == -1) {
perror("");
}else if (pid == 0) {
execvp(string[0], string); /* execute the command */
fprintf(stderr, "Failed to exec");
exit(1);
}
else {
//.. wait until the child ends
waitpid(-1, &status, 0);
}
return;
}
int read_input (void) {
int choice;
printf("Seleziona una voce dal menu");
do {
printf("\n");
scanf ("%i", &choice);
if (choice > 8 || choice < 1)
printf ("Attenzione, inserisci un valore contemplato dal menu");
}
while ( choice > 8 || choice < 1);
return choice;
}
void main (int argc, char *argv[]) {
printf ("------------------------\n");
printf (" MENU \n");
printf ("------------------------\n");
printf (" \n");
printf ("1) Esecuzione in foreground di un processo\n");
printf ("2) Ctrl -C\n");
printf ("3) Exit\n");
printf ("4) Background\n");
printf ("5) Pipe\n");
printf ("6) Jobs\n");
printf ("7) fg\n");
printf ("8) kill\n");
int menu = read_input();
switch (menu) {
case '1' :
exec ();
break;
case '2' :
//ctrl();
break;
case '3' :
//exit_();
break;
case '4' :
//background();
break;
case '5' :
//pipe();
break;
case '6' :
//jobs();
break;
case '7' :
//fg();
break;
case '8' :
//kill();
break;
}
}
這是警告:
elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " .");
關於與輸入有關的問題,
do {
printf("\n");
scanf ("%i", &choice);
if (choice > 8 || choice < 1)
printf ("Attenzione, inserisci un valore contemplato dal menu");
}
while ( choice > 8 || choice < 1);
鍵入整數並按Enter鍵后, scanf()
將使用該數字,並在stdin中保留換行符。 下次循環循環時(假設輸入<1或> 8或其他),scanf獲取該換行符並繼續。 在scanf()
之后添加一個getchar()
scanf()
。
答案在警告中,您應該將其從注釋移到問題中。
elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " .");
這意味着從整數初始化為char
指針的word
。 因此,似乎strtok()
返回的是整數……聽起來不太正確。
在strtok()
手冊頁中:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
看起來很正確,它返回一個char *
....但它還說它是在<string.h>
...中聲明的,您不包括在內。 由於未定義,因此編譯器將其假定為int strtok()
。
修復:添加#include <string.h>
行。
您看到的問題是scanf
被跳過了。
有關更多詳細信息,請參閱此處
在printf
之后對scanf()
的調用消耗了換行符,並且繼續進行而無需用戶輸入任何內容。
它從standard in讀取下一個字符,這可能是換行符,因此不會提示您輸入任何內容。
我太累了,對整個代碼注釋(這是非常糟糕的),所以我就寫有問題的問題,以及如何平凡它可以進行調試。
首先要做的是檢查我們是否獲得數字並返回到main,因此:
int menu = read_input();
printf("got [%d]\n", menu);
運行此:
[snip]
1
got [1]
所以我們確實到了這一點。
因此,現在我們檢查一下這是什么。
int menu = read_input();
printf("got [%d] '1'=[%d]\n", menu, '1');
讓我們運行:
1
got [1] '1'=[49]
因此, 菜單存儲了一個整數1,而“ 1”是ascii中1的字符代碼,而它並不是整數1。
總的來說,我認為縮小范圍是什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.