Why is this a infinite lopp? - infinite

#include <stdio.h>
int main(void)
{
for(int i = 0; i < 3; i++)
{
char buf[1];
sprintf(buf,"%i",i);
printf("%s\n",buf);
}
return 0;
}
Is there any reason for i to keep reseting to 0?
this has me really confused.

Related

From binary to string

I have to convert the binary to string, I tried something, but it didn't work.
This is my code (I don't allowed to change the main function!) , but I can't understand what's wrong:
#include <stdio.h>
#include <stdbool.h>
#include<math.h>
#include<string.h>
void decode_bytes(const int rows, bool bytes[rows][8], char string[rows]){
int iteration = 8;
for(int j = 0; j < rows; j++){
for (int i = 0; i < iteration; i++){
if (bytes[j][i] == 1){
string[j] += pow(2, iteration - (i + 1));
}
}
}
}
int main(){
bool bytes2[7][8] = {
{0,1,0,0,1,0,0,0},
{0,1,1,0,0,1,0,1},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,1,1},
{0,0,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0}
};
char string[7];
decode_bytes(7, bytes2, string);
printf("%s\n", string);
}
Output must be: "hello", but it's "cel<mB"....

C for condition tricks

I found a challenge on the internet and I'm really stuck.
The goal is to print 20 times _ by adding/changing only 1 character (only one operation performed in total):
#include <stdio.h>
int main(void)
{
int i;
int n=20;
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
I have already found 1 solution but I can't find the last one? Is there some tricks I need to know about for loops ?
Replace i by n
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i < n; n--)
printf("*");
getchar();
return 0;
}
Put - before i
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
printf("*");
getchar();
return 0;
}
Replace < by +
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i + n; i--)
printf("*");
getchar();
return 0;
}
Source: https://www.geeksforgeeks.org/changeadd-only-one-character-and-print-exactly-20-times/
to correct the posted code to only output 20 times, you could use:
#include <stdio.h>
int main(void)
{
int i;
int n=-20; // note the minus 20
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
If it is allowed you could write:
n=10; for(i=0;i<n;i++){printf("__");}
or
n=10; for(i=0;i<n;i++){printf("_");printf("_");}

multithreading - occasional segfault in matrix multiplication in c

I'm working on a small program of multithreaded matrix multiplication. My first job is to fill the entry of matrices with a random integer. I met some segment faults after I tried to pass a function pointer to pthread_create. And I think the problem is in function pthread_join.
But there are two issues in general.
The first one is the segment fault does not happen every time. Sometimes the code works, but most of the times it doesn't. So it really confuses me.
The other one is when the code is working, there are always several entries still not initialized, especially for matrix[0][0], it is never initialized. And I don't quite know where to debug that one.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#define N 5
#define MAX 10
int A[N][N];
int B[N][N];
int C[N][N];
pthread_t pid[N][N];
typedef struct {
int row, col;
} Pos;
typedef void* (*thread_func)(void*);
void print_matrix(int M[][N]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%3d", M[i][j]);
if (j < N - 1) {
printf(", ");
}
}
printf("\n");
}
}
void join_threads(void) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
pthread_join(pid[i][j], NULL);
}
}
}
void* fill_entry(void* arg) {
Pos* pos = (Pos*)arg;
A[pos->row][pos->col] = rand() % MAX;
B[pos->row][pos->col] = rand() % MAX;
return NULL;
}
void dispatch_jobs(thread_func job_func) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
}
}
}
int main(void) {
srand(time(NULL));
dispatch_jobs(&fill_entry);
join_threads();
printf("Matrix A:\n");
print_matrix(A);
printf("Matrix B:\n");
print_matrix(B);
return 0;
}
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
You are passing a pointer to a local variable to the threads. Once the thread tries to access the data, i.e. dereferences the pointer, the variable is long gone, reused, and contains garbage data.

Create an array of random numbers and return the array in C

I want to create a function called get_lotto_draw that will create an array of 6 random numbers and return them to main.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
get_lotto_draw()
{
int lottery[50];
int u,i,j,temp;
int lotto[6];
srand(time(NULL));
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void get_lotto_draw(int lotto[])
{
int lottery[50];
int u,i,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
int lotto[6];
srand(time(NULL));
get_lotto_draw(lotto);
for (int i = 0; i < 6; i ++)
printf ("%d ", lotto[i]);
printf ("\n");
return 0;
}
The function get_lotto_draw will take the array 'lotto' as an argument - unsized array.
If you declare lotto as auto variable inside the function then when the function ends - the auto variable lotto would have been removed - check this link for details.
So you declare lotto in main and pass it to the function.
Other options include
use malloc (to allocate memory for 'lotto') inside the get_lotto_draw and free in main (unless you are very careful- this will lead to memory leaks - I do not recommend this)
use malloc in main and pass the allocated memory to the function and free it in main later
create a static and use it.
My recommendation is in such situations to use the stack (auto variable as used above) otherwise use malloc/free.
I fixed some of the bugs others pointed out
Thank you very much for everyone who helped. I spent a few hours reading and learning about the things you mentioned. I have now managed to create a piece of code that will do what I wanted.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
int *lotto;
lotto = malloc(n * sizeof(*lotto));
if (lotto == NULL)
return NULL;
int lottery[50];
int u,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
srand(time(NULL));
lotto = get_lotto_draw(n);
if (lotto == NULL)
return -1;
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", lotto[i]);
}
free(lotto);
printf("\n\n");
return 0;
}
I also found that I can do this by using static int as I have an array of set length.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
static int lotto[6];
int lottery[50];
int u,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
srand(time(NULL));
lotto = get_lotto_draw(n);
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", lotto[i]);
}
printf("\n\n");
return 0;
}

stack overflow unless printf under recursion

This code below is the code for finding the determinant for 3x3 matrix (this code is intended for nxn matrix, but for the sample, I used 3x3), using recursive
The result is working fine, but I wonder what errors in this code make this must be the printf("\n") before calling the sub-function (itself) or else it will return the error 0xc0000fd (stack overflow).
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define size 3
void trimarray(int**rrayrc,int**rrayout, int dim,int cuti,int cutj)
{
int i, j;
int ti = 0,tj;
for(i = 0; i<dim; i++)
{
tj = 0;
for(j = 0; j< dim; j++)
{
if(!((i==cuti)||(j==cutj)))
{
rrayout[ti][tj] = rrayrc[i][j];
}
if(j!=cutj) {tj++;}
}
if(i!=cuti) {ti++;}
}
}
void initializearray(int** rray,int dim)
{
int i, j;
for(i = 0; i<dim; i++)
{
for(j = 0; j<dim; j++)
{
rray[i][j] = 0;
}
}
}
int det(int** rray, int dim)
{
int i,j;
int cut[dim-1][dim-1];
int* cutp[i];
int mul = 1,sum=0;
if(dim >1)
{
for(i = 0; i<dim-1; i++)
{
cutp[i] = cut[i];
}
initializearray(cutp,dim-1);
for(i = 0; i<dim; i++)
{
printf("\n",dim); //<< Without this the program won't work
trimarray(rray,cutp,dim,0,i);
sum+=det(cutp,dim-1)*mul*rray[0][i];
mul = 0-mul;
}
return sum;
}
else
{
return rray[0][0];
}
}
int main()
{
int test[size][size] = {2,-3,-2,-6,3,3,-2,-3,-2};
int* testpntr[size];
int i,deter;
for(i = 0; i<size; i++)
{
testpntr[i] = test[i];
}
deter = det(testpntr,size);
printf("[%d]",deter);
getch();
return 0;
}
The answers will be dearly appreciated.
int* cutp[i]; is undefined behavior since i is uninitialized at this stage. You have no idea what is the size of cutp array.

Resources