Why i am not able to change the dummy variable here in my code?

darxtrix

OK, this is a simple bit of procedural code i am trying to write for achieving the functionality of atoi function. The thing bothering me is that i am unable to change the dummy variable in the if block while i can change the n variable.The n and dummy are defined in the same scope.

int main()
{

    char *s;

    puts("Enter a string");
    fgets(s,100,stdin);
    int n = 0 , dummy = 1; // both are defined in the same scope 

    for(int i = 0 ; i < 2 ; i++ )
    {
        if(isdigit( s[i] ))    { n = n*10 + (s[i] - '0'); }

        if(1)
        {
            dummy = dummy + 99; // This is the thing not working , program stops executing
            // n = n + 99;  while this works fine
            printf("%d\n", dummy );
        }
    }
    printf("%d", n);
}

I can print the dummy variable fine but i am not able to assign it a value and when i tried to do that program stops working.If i comment the line involving dummy and uncomment the below line using n, the program runs fine.

Don't mess with the logic.So what's happening ?

Sakthi Kumar
char *s;

s needs memory to be allocated either statically or dynamically

char s[100];

or

char *s = NULL;
s = malloc(100);
if (s)
    /* do operations with s */
.
.
if (s)
    free(s);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I not able to change the value of the boolean variable in this method

I am not able to getdownloadurl() from firebase. Please anybody help here's my code and error. (I am using Fragment)

I am not able to use jAlert in my code ?

Why i am able to change angular constant?

Why am I able to change the array

Why I am not able to change the state of my react component the second time and got no error

Why I'm not able to trigger the "change" event here?

I am not able to change the position of my ck editor

I am not able to change the hour and minutes of a DateTime variable

How am I able to access the Firebase variable AcX in my user?

Here is my final code. I am getting in the end the error message multiple times and I don´t know why

Why am I not able to change UIButton text-color?

Why am I not able to change the legend title for this sf object plotting?

Why am I not able to change the shell with the chsh command?

Why i am not able to call adapter method from my activity

Why am I not being able to insert values into my SQL database?

Why I am not able to receive data in my adapter

Why am I not being able to connect to my database?

Why am I able to instantiate my Abstract Base Class in Python?

Why I am not able to refer to my sequence with double quotes in postgres?

Why am i not able to push files into My Git Repo

Why am I not able to use promise in my basic react app?

Why am I not able oneHot encode my data labels?

Why am I not able to access the REST API exposed by my service?

Why am I not able to get proper dates in my graph?

Why am I not able to pickup items in my text based game?

Why am I not able to set cookies on my device?

why i am not able to update env variable in node js

Why am I able to global a non-existing variable in python