user1877339
user1877339

Reputation: 5

infinite loop on printing 2D array

#include <stdio.h>


void main()
{
    char plaintext[50];
    char key[20];
    int plain=0,max1=0,max2=0; // max2 amount of coloumn n max1 for row on chip
    char chip[30][30];
    int i,j,k=0,c=0;

    printf("Enter key :");
    scanf("%s",&key);

    for(i=0;key[i]!='\0';i++)
    {
        max2++; 
    }

    printf("Enter plaintext :");
    scanf("%s",&plaintext);

    for( i = 0; plaintext[i] != '\0'; i++ ) 
    {
        plain++; 
    }
    if (plain%max2==0)
    max1=plain/max2;
    else
    max1=plain/max2+1;
    while(plaintext[k]!='\0')
    {

        for (i=0;i<max1;i++)
        {
            for (j=0;j<max2;j++)
            {
            chip[i][j]=plaintext[k];
            k++;
            }
        }
    }
    printf("%s",chip[0][0]);
}

1st im trying to move the string on plain (1D array) into chip(2d array) with dynamic array on the 2D but when im trying to run this code it show nothing than stopped working.. is there anything goes wrong with my 2D array?

Upvotes: 0

Views: 309

Answers (1)

boaz_shuster
boaz_shuster

Reputation: 2925

I compiled it on Linux (after omitting the void because in Linux main has to return a value) and I got "Segmentation fault". I guess it is because of printf("%s",chip[0][0]);, it should be printf("%s",chip[0]);

I mean chip[0][0] returns a specific character in the chip[0] and you want to print the first string in chip array, right?

Upvotes: 1

Related Questions