PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] Why does the array store different unwanted numbers in the given problem?? This causes false outputs

 August 18, 2022     arrays, c, function, output     No comments   

Issue

I want to use the memcmp() function in the program. But unwanted numbers get stored in the arr4 array variable when I assign it values passed from another function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int print(int arr4[], int arr5[], int arr6[]);

int main(int argc, char const *argv[])
{
    int arr1[4] = {1, 2, 3, 4}, arr2[4] = {5, 6, 7 ,8}, arr3[4] = {9, 10, 11, 12}, arr4[4], value;
    arr4[3] = print(arr1, arr2, arr3);
    arr4[2] = 5;
    printf("\n\nElements in arr4 array: \n");
    for(int i = 0; i < 4; i++)
        printf("%d ", arr4[i]);
    printf("Press Enter...\n");
    getchar();
    value = memcmp(arr4, arr3, 4 * sizeof(int));
    printf("\nThe value is: %d\n", value);
    if(value == 0)
        printf("\nThe arr3 and arr4 are the same!!!\n");
    else if(value < 0)
        printf("The arr3 is greater than arr4!!!\n");
    else if(value > 0)
        printf("The arr4 is greater than arr3!!!\n");
    printf("\nDone with the print program!!\n\n\a");
}

int print(int arr4[], int arr5[], int arr6[])
{
    int num[4];
    memcpy(num, arr4, 4 * sizeof(int));
    printf("The First 4 numbers stored: \n");
    for(int i = 0; i < 4; i++)
        printf("%d ", num[i]);
    printf("\nThe second set of numbers stored: \n");
    memcpy(num, arr5, 4 * sizeof(int));
    for(int i = 0; i < 4; i++)
        printf("%d ", num[i]);
    printf("\nThe third set of numbers stored in the array: \n");
    memcpy(num, arr6, 4 * sizeof(int));
    for(int i = 0; i < 4; i++)
        printf("%d ", num[i]);
    return num[3];
}

Output:

The First 4 numbers stored: 
1 2 3 4 
The second set of numbers stored: 
5 6 7 8 
The third set of numbers stored in the array: 
9 10 11 12 

Elements in arr4 array: 
-465963304 32766 5 12 Press Enter...


The value is: 207
The arr4 is greater than arr3!!!

Done with the print program!!

-465963304 32766 these are the unwanted numbers that get stored and cause problems in the program output. Why do these come? Also, when I compile and run it the second time, -465963304 changes to another number: -408901928 and keeps changing when I compile and run it another time...

However, when I pass the arr4 array variable to the void function print(), the output comes as desired...

What is the problem with the first version of the program? In addition to these, when I return num[4], the suggestion comes as

warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
    return num[4];
           ^   ~
note: array 'num' declared here
    int num[4];
    ^

But there is no warning when I have it as return num[3].
Why does this happen?


Solution

you are not initialize arr4 array,so the value storage in arr4 is random.int arr4[4]={0};



Answered By - Jason Lee
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing