Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't this printf() statement print two string variables in C?
    primarykey
    data
    text
    <p>I have been researching like mad while I learning C. I have been debugging a C program and I thought I had some major issues <a href="https://stackoverflow.com/questions/11747579/why-is-fgets-and-strncmp-not-working-in-this-c-code-for-string-comparison">here</a>. Now I have critical issues. I made a dummy program to print two strings in one statement as follows: </p> <pre><code> #include&lt;stdio.h&gt; int main(int argc, char* argv[]) { char *herp = "Derp"; char *derp = "Herp"; printf("Herp %s Derp %s\n", herp, derp); return 0; } </code></pre> <p>This prints out as expected. I get </p> <pre><code>Herp Derp Derp Herp </code></pre> <p>So, I thought, let me debug my own program by doing something similar. The following line in my program</p> <pre><code>printf("word is: %s and jumbled word is: %s\n", word, jumbleWord); </code></pre> <p>Should print out something like </p> <pre><code>Word is: word and jumbled word is: dowr </code></pre> <p>But it prints out something like</p> <pre><code>and jumbled word is: dowr </code></pre> <p><em>Where did the first part of the output go</em>? I need to be able to print these both on the same line to debug. Also, the fact that a statement like this is not working tells me that really weird things are going on and I am bald from tearing my hair out. As my linked posts indicates, I would eventually like to compare these string values, but how can I even do that if printf() is not working right? </p> <p>I am posting the entire program below so you can see where everything is happening. I am just learning how to use pointers. I originally had two pointers point at the same memory when I wanted to jumble a word and that didn't work very well! So I fixed that and got two separate memory spaces with the words I need. Now, I just can't print them. This all makes sense given the code below: </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #define MAX_WORD_LENGTH 25 //Define global variables int numWords; //Preprocessed Functions void jumblegame(); void readFile(char *[]); void jumbleWord(char *); void guess(char *,char *); int main(int argc, char* argv[]) { jumblegame(); return 0; } void jumblegame() { //Load File int x = 5050; //Rows char *words[x]; readFile(words); //Define score variables int totalScore = 0; int currentScore = 0; //Repeatedly pick a random work, randomly jumble it, and let the user guess what it is srand((unsigned int)time(NULL)); int randomNum = rand() % numWords + 1; char source[MAX_WORD_LENGTH + 1]; char jumble[MAX_WORD_LENGTH + 1]; strncpy(source, words[randomNum], MAX_WORD_LENGTH + 1); strncpy(jumble, words[randomNum],MAX_WORD_LENGTH + 1); jumbleWord(jumble); guess(source, jumble); //printf("Random word is: %s\n ", words[randomNum]); //randomly jumble it } void readFile(char *array[5049]) { char line[256]; //This is to to grab each string in the file and put it in a line. int z = 0; //Indice for the array FILE *file; file = fopen("words.txt","r"); //Check to make sure file can open if(file == NULL) { printf("Error: File does not open."); exit(1); } //Otherwise, read file into array else { while(!feof(file))//The file will loop until end of file { if((fgets(line,256,file))!= NULL)//If the line isn't empty { int len = strlen(line); if (len &gt; 0 &amp;&amp; line[len - 1] == '\n') line[len - 1] = '\0'; array[z] = malloc(strlen(line) + 1); strcpy(array[z],line); z++; } } } fclose(file); numWords = z; } void jumbleWord(char *word) { int wordSize = strlen(word) - 1; //durstenfeld Implementation of Fischer-Yates Shuffle int i; int j; char temp; for(i = wordSize - 1; i &gt; 0; i--) { j = rand() % (i + 1); temp = word[j]; word[j] = word[i]; word[i] = temp; } } void guess(char *word, char *jumbleWord) { printf("original word is: %s\n", word); printf("jumbled word is: %s\n", jumbleWord); printf("source is: %s and jumbled word is: %s\n", word, jumbleWord); } </code></pre> <p>I think most people at this point would burn C and slap themselves for sucking so bad at it. However, I am going to keep trucking along. So let me apologize for any derp, but please know that I have spent many hours probably being really stupid and staring at this. I would love to say, "<em>Hey, C is so stupid because it won't do what I am telling it to</em>". Unfortunately, I can't believe this. I think it is doing exactly what I am telling it to do, but I am too close to this problem to see what I am doing wrong. </p> <p>As always, thank you for your help. My deepest respect, GeekyOmega</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload