LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting from c++ to c

// I having trouble converting my c++ program to c.

//NON-FATAL RUN-TIME ERROR:   "test c files.c", line 24, col 13, thread id 0x000013E8:   Array index (17675088) too large (maximum: 5).
//NON-FATAL RUN-TIME ERROR:   "test c files.c", line 24, col 13, thread id 0x000013E8:   Array index (17675088) too large (maximum: 5).
 

 

#include <ansi_c.h>
#include <utility.h>
#include <stdio.h>

void binary(int);

void main(void) {
int number;

printf("Please enter a positive integer: ");
scanf("d%",&number);
if (number < 0)
printf("That is not a positive integer.\\n");
else {
printf("%d",number," converted to bits is: ");
binary(number);

//return 0;
}
}

void binary(int number) {
char temp[][10]={"00000000","00000001","00000010","00000100","00001000","0001000"};
printf("%d",temp[number]);

GetKey();

}

0 Kudos
Message 1 of 5
(4,292 Views)

I suspect that since your declaration of the temp array is incomplete (first dimension omitted) it is established by the number of items in the initializer (Six ten-character array elements, zero based, 0 -> 5).

 

So if user asks for a number greater than 5 and you try to index into the array with it you'll have walked off the end of the temp array and you get a runtime error.

0 Kudos
Message 2 of 5
(4,286 Views)

//this is the changes, no errors but if i enter in a number between one through 5 its only printing out the first string

//which is 00000000.

//its suppose to do this :if i enter 1, it prints out 00000001, if i enter 2 ,it prints 00000010.

 

#include <ansi_c.h>
#include <utility.h>
#include <stdio.h>

void binary(int);

void main(void) {
int number=0;

printf("Please enter a positive integer: ");
scanf("d%",&number);
if (number < 0)
printf("That is not a positive integer.\\n");
else {
printf("%d",number);
binary(number);

//return 0;
}
}

void binary(int number) {
char temp[][10]={"00000000","00000001","00000010","00000100","00001000","0001000"};
printf("%s",temp[number]);

GetKey();


}

0 Kudos
Message 3 of 5
(4,277 Views)

Darnell:

 

You have the format string wrong on your scanf statement.  The format string should be "%d", not "d%".

 

Before you correct it, try some basic debug.  set a breakpoint after the scanf statement, then look at the value of number.  You'll see that it is 0.

 

Basic debugging here starts by evaluating what your output is, and then determining where it came from.  If you set a breakpoint in your binary function,  you would see that number was always 0, which is why you always returned "00000000".  If you would have looked at your output, you would have seen that your number was 0 as well.

 

Please don't continue to post the same question to multiple threads.  You posted this same question on c++ conversion to your thread on pass/fail test.

0 Kudos
Message 4 of 5
(4,266 Views)

The "d%" does seem to be the problem here. In addition, in your next line with the printf() you probably just want a "\n" instead of your double backslash "\\n". Also note, this isn't really converting C++ to C as it is basic debugging of your code. As AI S suggested, use your compiler and debugging tools to your advantage. If you need some help getting started with debugging here's a short concise tutorial with lots of techniques

 

How to Debug Programs

 

Regards,

 

Steven Zittrower

Applications Engineer

National Instruments

http://www.ni.com/support

0 Kudos
Message 5 of 5
(4,235 Views)