2016-05-12 191 views
2
#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 

    int agecalc; 
    int nextyr; 
    int birthday; 
    int currentday; 
    int agecalcu; 
    int randomnumbers; 

    birthday = 1987; 
    currentday = 2016; 
    nextyr = currentday + 1; 
    agecalc = currentday - birthday; 
    randomnumbers = 7890; 

    char My[] = "Sayan Banerjee"; 

    printf("%s 's birthday is %.3d \n", My , agecalc); 

    agecalcu = agecalc + 1; 

/* alternatively I can use one int and change the variable stored in agecalc by stating that 
agecalc = currentday - birthday +1; But I think that is just messy and disturbs a lot of code. declaring a new int is 
better and less intrusive. this way when I may need to use agecalc again, i dont have to worry about the value stored in that 
variable. */ 

    printf("%.5s will turn %.3d on %d.\n \a", My , agecalcu , nextyr); 

    printf("The username for %s is %.6d \n", My , randomnumbers); 
    // the thing here is that I cannot shorten a value stored in a value unless its in an array 
    // but I can express it in other ways like 1 to 01. 
    /* The thing is that I cannot store two %s in one argument/code. this freezes the code. better way would be to 
    create another variable and then try to edit that.*/ 

    //this is an experiment to see whether I can take characters from an array and store it in a variable 
    int user; 


    My [0,1,2,3,4,5] = user; 

    printf("%s is also %s", My , user); 


    return 0; 
} 

我的主要問題是在最後一行。我是一名編程新手,剛剛開始學習。我只是在玩耍,注意到如果我在同一個參數中輸入兩個%s,程序就會崩潰。所以,我在考慮是否可以將數組My中的某些字符存儲在變量中,然後將其打印出來?如何從數組中獲取字符並將其存儲在變量中?

可能嗎?或者我只是以錯誤的方式看待這個問題? 對不起,這個雜亂的帖子。 感謝您的幫助。

+0

也有意見,只是我發現周圍的修修補補。我寫的內容可能是錯的,但我只是把整個事情都貼出來。謝謝你的耐心! – returnNULL

+0

歡迎來到StackOverflow。請閱讀[格式化頁面](https://stackoverflow.com/editing-help),這樣你的問題就不會像「混亂」一樣。另外請記住,你總是可以[編輯]你的問題。 – Zeta

+0

順便說一句,如果'x = y'將'y'的值賦給''x'',你的'My ... = user'對你有意義嗎? (我現在沒有時間給出完整的答案,對不起)。 – Zeta

回答

1

My [0,1,2,3,4,5] = user;

這一行相當於說

My[5] = user;

這是因爲comma操作

//this is an experiment to see whether I can take characters from an array and store it in a variable 

你可以的。但是你現在正在做的是將一個未分配的值分配給My陣列的5th元素。 int val = My[5]是完全正確的,而不是其他方式。

飛機墜毀發生,因爲你的代碼試圖解釋一個整數作爲null terminated string因爲%s格式說明你目前的狀態給

printf("%s is also %d", My , user); // %d for integers 

你的代碼是在Undefined Behavior土地。

若要將現有數組中的某些字符複製到目標數組中,您將需要多個變量。一個變量只會保存一個字符。

char user[6]; 
user[5] = '\0'; // fill the last character as null to be a valid C string 
user[0] = My[0]; 
user[1] = My[1]; 
user[<any other index you want>] = My[<The index you want>]; 

您可以通過簡單地使用從string.h其中有一堆字符串處理函數的實用程序保存自己的精力,時間和精力。可能你目前的問題需要的是strncpy

作爲一個正交的建議,編譯你的代碼時使用編譯器支持的最高警告級別。編譯器會在編譯時提出這些問題。

對於如gcc -Wall filename.c

+0

當我嘗試使用int val = My [5]時,輸出結果爲Sayanpbanerjee也是@。如果我採取你的其他想法,那麼輸出是Sayanpbanerjee也是4200816. 現在,我可能是錯的,但是,我們不能保存「字符串或文本」在一個int變量?那爲什麼這給了我這樣一個奇怪的輸出? – returnNULL

+0

順便說一句,英語不是我的第一語言,所以我在解釋事情時有點絆倒。代碼的最初意圖是在數組中使用某些字母,然後將它們放入一個變量中,然後嘗試在代碼中運行它。 – returnNULL

+0

@SayanBanerjee更新了我的答案以解決您的評論。 – bashrc

0

行之後不會做你認爲:

這樣的:

My [0,1,2,3,4,5] = user; 

其實是同樣的事情,這樣的:

My [5] = user; 

閱讀有關comma operator

無論如何,您不能將整個My數組存儲在單個int變量中。


這裏使用的是%s打印user這不是一個char*而是int

printf("%s is also %s", My , user); 

這將導致不確定的行爲,很有可能是在現代系統崩潰。


至於你的問題實在是有點不清楚,你也許想要這個:

#include <string.h> 
... 
char user[20]; 
strcpy(user, My); 

printf("%s is also %s", My , user); 

,它將打印:

Sayan Banerjee is also Sayan Banerjee 
+0

首先要做的事情。感謝所有的幫助!我對strcpy有一點了解,我不知道爲什麼我自己沒有使用它。以及代碼的作品,我可以保持它15.爲什麼你指定20?這不是我最初想要的,並且將用戶的字節大小編輯爲低於15的任何值都不會提供我期待的結果。 我正在尋找的是從數組中取出某些字符:也許是B,N,J,R,S並將其放入變量中。有沒有辦法做到這一點?再次感謝您的耐心等待! – returnNULL

相關問題