Thread: Warning C4700 uninitialized variables?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question Warning C4700 uninitialized variables?

    Hello All,
    i am taking an engineering computer course which teaches c programming for beginners so i know almost nothing, i need to make a program that calculates x^n recursively without using the pow function. When i debug i get two warning messages saying (Warning C4700) that x and n are uninitialized local variables and i have to abort the program.

    this is the code that produces the error;

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    	int x,n,ans;
    	printf("input two positive values\n");
    	scanf("%d" "%d",x,n);
    	ans=x;
    {
    	while(n>0);
    	ans=ans*x;
    	n=n--;
    }
    	printf("%d\n",ans);
    }
    i think it should work but i don't understand why this error is coming up.
    Thank you in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to take the addresses of x and n for scanf.

    By the way, after you fix your indentation, it might become more obvious that your while loop is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    We have a little bit of oopsidaisies. As laserlight said, you want to pass the address of x and n. (That let's scanf modify their values). I did the simplest fix on your loop (and then had to remove the semicolon because it was an infinite loop otherwise). I came up with

    Code:
    #include <stdio.h>
    int main()
    {
    	int x,n,ans;
    	printf("input two positive values\n");
    	scanf("%d" "%d",&x,&n);
    	ans=x;
    	while(n>0)
    	{
    
    		ans=ans*x;
    		n=n--;
    	}
    	printf("%d\n",ans);
    }
    And I ran it:
    Code:
    input two positive values
    2 3
    16
    Oopsies. Change line 7 to read ans = 1;

    Code:
    #include <stdio.h>
    int main()
    {
    	int x,n,ans;
    	printf("input two positive values\n");
    	scanf("%d" "%d",&x,&n);
    	ans=1;
    	while(n>0)
    	{
    
    		ans=ans*x;
    		n=n--;
    	}
    	printf("%d\n",ans);
    }
    Prior to that your code was computing x^(n+1). I have some more thoughts. One, this isn't recursive, it's actually iterative. So when you turn it in it will compute correctly and still be wrong because it's not what they asked for. I won't post the code of the solution, but take it as a hint that there is an inductive definition of power(a, b):

    power(a, 0) = 1
    power(a, n) = a * power(a, n-1)

    Second, do you understand now how scanf works? The idea is you put in a format string, then you follow that with a list of the references of the variables you would like to put the read values into. So let's say I'd like to read an int and then a string:

    scanf("%d %s", &myInt, myStr); // myStr is a reference already, so &myStr would be wrong, in fact it will seg fault:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int x;
    	char str = malloc(sizeof(char)*16);
    	scanf("%d %s", &x, &str); // Oops, wrong to take address of str as str is already and address
    	printf("%d, %s", x, str);
    	free(str);
    }
    Output:
    Code:
    ./a.out
    2 34lk
    Segmentation fault
    Reason why: scanf will attempt to write the string I enter into a pointer value which is wrong, because it's not in my address space:

    Code:
    valgrind ./a.out
    ==2378== Invalid read of size 1
    ==2378==    at 0x4E777AA: vfprintf (vfprintf.c:1614)
    ==2378==    by 0x4E7E6F9: printf (printf.c:35)
    ==2378==    by 0x400643: main (foo.c:9)
    ==2378==  Address 0x68 is not stack'd, malloc'd or (recently) free'd
    ==2378== 
    ==2378== 
    ==2378== Process terminating with default action of signal 11 (SIGSEGV)
    ==2378==  Access not within mapped region at address 0x68
    ==2378==    at 0x4E777AA: vfprintf (vfprintf.c:1614)
    ==2378==    by 0x4E7E6F9: printf (printf.c:35)
    ==2378==    by 0x400643: main (foo.c:9)

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thank you very much, now that i put in the &x and &n for scanf i can fix the programs mathematical issues. Thank you both so much, and thank you QuatdraticFighte for the scanf explanation.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    QuadraticFighte:
    You forgot to make str an actual pointer.

    char str = malloc(sizeof(char)*16);
    should really be
    char* str = malloc(sizeof(char)*16);

    But then again, you really don't need dynamic memory at all, so

    char str[16];

    would suffice.
    I also urge you to read SourceForge.net: Scanf woes - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Elysia View Post
    QuadraticFighte:
    You forgot to make str an actual pointer.

    char str = malloc(sizeof(char)*16);
    should really be
    char* str = malloc(sizeof(char)*16);

    But then again, you really don't need dynamic memory at all, so

    char str[16];

    would suffice.
    I also urge you to read SourceForge.net: Scanf woes - cpwiki
    My mistake, that was a bad copy-paste. The reason for malloc was that I wanted a contrived way to make sure I would go out of my address space. I don't know how to do that if I am on the stack.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "would go out of my address space"?
    You mean don't go out of address space?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Elysia View Post
    "would go out of my address space"?
    You mean don't go out of address space?
    No, I don't. I wrote a program to seg fault on purpose to illustrate why if you are going to scanf("%s") you put it into a char* string, passing string rather than &string, because we don't want to pass the address of the string.

    The purpose was to illustrate the difference between
    Code:
    char* myString;
    scanf("%d", &myInt); // and the following...
    scanf("%s", string); // Which is correct, versus
    scanf("%s", &string); // Which is incorrect
    What I did was intentional.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, I just didn't understand your wording in your previous reply.
    The example is correct, provided you've initialized MyString with a valid address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  2. Malloc access violation
    By JOCAAN in forum C Programming
    Replies: 7
    Last Post: 11-30-2008, 04:47 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM