During a university project, I had to print some int arrays to the screen, and I wanted to do it with printf.
So I decided to include a function that converts an integer array into a string type, and passes the string out to the calling function.
But the function in question crashes the program after calling it for the second time in show().
And I can't figure out why... Any help would be appreciated.

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

#define SIZE_A      30

unsigned arraylen (int _a[])	/*(Inserted Int Array)*/
{
    unsigned int _cntr=0;
    
    while (_a[_cntr]!='\0' && _cntr<SIZE_A)		//
		_cntr+=1;
    return _cntr;
}

char Int2Str (int _rrA[SIZE_A], char _XtStr[SIZE_A])	/*(Inserted Array)*/
{
	unsigned _cnt=0;
	
	while (_XtStr[_cnt]= (isdigit(_rrA[_cnt]) && _cnt<arraylen(_rrA)) ? _rrA[_cnt] : '\0')
		_cnt+=1;
	return;
}

void show (int _InSq[SIZE_A], int _KSq[SIZE_A], int _OutSq[SIZE_A], char _cs)	/*(String for Output, Key String, String from Input, Choice)*/
{
	char _InStr[SIZE_A], _KStr[SIZE_A], _OutStr[SIZE_A];
	
	Int2Str(_InSq, _InStr);
	Int2Str(_KSq, _KStr);
	Int2Str(_OutSq, _OutStr);
    if (_cs=='q' || _cs=='Q')
		printf("\nThe number %s encoded with the key %s is %s.\n\n", _InStr, _KStr, _OutStr);
	else if (_cs=='w' || _cs=='W')
		printf("\nThe number %s decoded with the key %s is %s.\n\n", _InStr, _KStr, _OutStr);
		 else {
					puts("Shit");
					puts("\t->This error derived from a misconduct during the printing function");
			  }
	return;
}

int main (void)
{
     int shh, i=0, array1[SIZE_A], array2[SIZE_A], array3[SIZE_A];
     char chRA[SIZE_A], choice, throwaway;
     puts("Choice:");
     choice=getchar();
     throwaway=getchar();
     puts("Array 1");
     while (array1[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     puts("Array 2");
     while (array2[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     puts("Array 3");
     while (array3[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     Int2Str(array1, chRA);
     puts(chRA);
     system("pause");
     show(array1, array2, array3, choice);
     system("pause");
     return;
}

The problem appears at line 30 while calling Int2Str() for second time while in show().

Dani AI

Generated

Quick diagnosis for : the crash you see on the second call to the converter is most likely caused by out‑of‑bounds writes when the three input arrays are filled. The input code reuses the same index variable for all three reads and never resets it, so the second and third loops start writing at the wrong offsets and can corrupt nearby stack memory (including the char buffers used in show). Fixing the input loops so each array gets its own index (or resetting i to zero before each loop) will often eliminate the crash immediately.

A few other corrections to make the code safe and maintainable: give the converter a proper return type (or void) instead of declaring char and returning nothing; always check the index against the array length before dereferencing (bounds-check first, then access); when calling isdigit cast the argument to unsigned char to avoid undefined behavior; and prefer passing explicit array lengths into helper functions rather than relying on scanning for a '\0' sentinel inside an int array (mixing ASCII digit codes and a zero sentinel is brittle).

A safer conversion function (replacement for your converter) — it validates bounds, casts for isdigit, and guarantees a NUL-terminated destination:

void int_array_to_cstring(const int *src, size_t src_len,
                          char *dst, size_t dst_len)
{
    size_t i = 0;
    while (i < src_len && i + 1 < dst_len && src[i] != 0
           && isdigit((unsigned char)src[i])) {
        dst[i] = (char)src[i];
        i++;
    }
    dst[i] = '\0';
}

Practical debugging tips: follow and enable strict compiler warnings (gcc/clang -Wall -Wextra -Wconversion or MSVC /W4), and run with AddressSanitizer (-fsanitize=address,undefined) or Valgrind to catch overruns. Also consider the simpler approach suggested — print the array directly in a loop when you only need output. Checklist: reset your indices, null‑terminate, check bounds before access, cast for isdigit, and use sanitizers.

Recommended Answers

All 5 Replies

Take a step back and ask yourself, WHY?

You can use printf() to print out your int array, in a loop, and control just exactly how you want it to appear. So why change the data type, at all?

But let's say you want to really use a char for printing out your int's. What are you going to do when your small range of a char, is exceeded?

I'm just bothered by the choice to go from the direct, to some circuitous route just to print up an array.

Rube Goldberg would love this, but it really bothers me. Google that name if you don't know what it stands for.

commented: I thought about the Rube Goldberg thing too. +3

I know, you are right saying that the other way it is more simple and all, but the point is why isn't this one working.
I used the printf in loop, and it is working quite well, but...
It should be working with the Int2Str function.
And I just wonder why it doesn't.

When a function says it shall return a char, and it returns nothing instead. I don't have a lot of confidence that it's working right. ;)

then there's this:

while (_XtStr[_cnt]= (isdigit(_rrA[_cnt]) . . .

Which should be:

while (_XtStr[_cnt]==(isdigit(_rrA[_cnt]) . . .

and a few other compiler errors and warnings. You should at least try to compile your code, and pay attention to the errors that stop it from compiling. There are some other errors, but I'm not doing your homework for you.

If you can't be bothered to pay attention to your compiler errors, there's no reason why I should do it for you.

Compiler does not show any errors...
I dont think I would ask for insight if I had something to work with.

Thanks

then you need to set your compiler to be more strict with the warnings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.