C - Values under the same array index are different [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am analyzing a C code that I have been given and I came across this block:
for (k=0; k<m; k++)
{
//Perform some calculation and assign result to
//A[k].
if (A[k]!=A[k])
{
exception=1;
}
}
I have performed runs of the code where exception does turn out to be one, but I can't seem to understand how two array indices can contain different numbers! Is that something to do with machine precision? Thank you!

You might want to check whether the A[] array is allocated sufficient amount of memory: it should have 'm' elements allocated at least. If everything is OK, check the sizes of other arrays allocated in your program. The phenomenon that you've encountered looks like some memory allocation error.

Related

Count amount of words, characters, newlines in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I know it is not recommended to post code as an image, since I am getting a formatting error when trying to post code on here. Anyway, I am trying to write a simple C program to count number of words, characters, newlines using do while loop. However, the output is not as expected. Please help!
Instead of a do-while, you should try using while. Since your code starts off by checking whether a is any of your if cases, it goes to the else case, and increments the New line variable. If possible, could you share the output screen.

After writing to a place in mmapped memory, when printing it out afterwards, it's not written anymore [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to mmap a bunch of integers in a multdimensional array into mmapped memory. I know the calculations work because I printed out the multidimensional array and it prints the correct values. And I compared the values in the double for loop, printing the value of the multidimensional array and the value at the place in mmapped memory after it's been assigned, and they both correspond to the same values.
When I leave the for loop and print out what's in the mmapped memory, only the first and last values print correctly. In contrast, when I print out the multidimensional array directly, it does print the correct values. Am I writing to the mmapped memory wrong?
I don't think this
199 args->shmem3[r+c] = m[r][c];
Is what you want. Rather:
199 args->shmem3[r*args->matrix_size+c] = m[r][c];

Weird issue in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
thanks for your help.
I'm trying to similate a simplified version of ARM, and I have a very weird error in c http://pastebin.com/3XRdngty .
I don't understant why in the function executer_code(),
the for doesn't work ...
I mean it should be looping untill the variable "i" is equal to the variable nombre_instruction, but it turns out that the variable "nombre_instruction" is the right value the first time it goes in the for, but the second time it doesn't go in the for because its value changed to 0, I search on the internet if someone has the same kind of error, and i didn't find anything.I reread my code but still I cannot figure out why it does this, 3 hours has passed already.
And thank you again for you help :)
This is taken from your code:
char *compar;
if(i==0){
sprintf(&compar,"%c%c%c%c",code[0],code[1],code[2],code[3]);
}
The problem here is that you declare compar as a pointer to char, but it is uninitialized. So, it has an undefined value. When you fill it with sprintf, you just write somewhere in the memory, and apparently, you write over the variable nombre_instruction.
Solution:
char compar[200];

Converting a character into a space in C arrays [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So lets say I have received a message that resembles the following
"L2N5*8R10!11T0A1K3Y14#4W7O6O9C12R13"
and I am expected to sort out the characters in accordance to the numbers succeeding them and change the characters that are not letters into a space. I have no problem doing the sorting out part, I am only having a trouble while trying to write a function that will change those characters into space.
The out put should be something like this
TALK NOW OR CRY
but I am getting
TALK#NOW*OR!CRY
Can anyone help me figure out what my function should look like so that I can be able to change the characters into space??
Unless you show your code, we'll only be able to guess!!
However, as a general suggestion, I would recommended, you should check each entry against isalpha(). In case you got a return value of 0, replace the entry with a .

How to allocate an array with NULL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can someone tell me how should I do to allocate every v[i].word and next with NULL?
Struct hash{
Char*word;
Hash*next;
}*v[10];
Make a for loop, let it iterate from i = 0 to 9. In each iteration, allocate one struct hash and point v[i] to it. Afterwards, initialize *v[i] as needed. Don't forget to check if malloc() failed.

Resources