Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:45:57+00:00 2026-06-17T10:45:57+00:00

EDIT: I have fully fixed my code now. Here it is: #include <stdio.h> #include

  • 0

EDIT: I have fully fixed my code now. Here it is:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main(){
    int n, i , j;
    char **mensagem;
    char leitura[1000];
    FILE *arquivo;

    arquivo = fopen("entrada.txt", "r");

    fgets(leitura, 1000, arquivo);

    fclose(arquivo);

    arquivo = fopen("entrada.txt", "r");

    n = sqrt(strlen(leitura));

    mensagem = malloc(n * sizeof(char *));

    for (i = 0; i < n; i++){
        mensagem[i] = malloc(n * sizeof(char));
    }

    for (j = 0; j < n; j++){
        for (i = 0; i < n; i++){
            fscanf(arquivo, "%c", &mensagem[i][j]);
        }
    }

    for (j = 0; j < n; j++){
        for (i = 0; i < n; i++){
            if(mensagem[j][i] == '*'){
                printf(" ");
                i = n - 1;
            }
            else {
                printf("%c", mensagem[j][i]);
            }
        }
    }

    fclose(arquivo);
    free(mensagem);

    return 0;
}

The idea is to decypher messages like Cesar did back in the days :D. I read a coded message from a .txt file. I read the entire line then I dynamically allocate an array of arrays.
That way I can print the columns and the message will be printed normally.

Here’s my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(){
    int n, i , j;
    char **mensagem;
    char leitura[1000];
    FILE *arquivo;
    arquivo = fopen("entrada.txt", "r");

    fgets(leitura, 1000, arquivo);

    n = sqrt(strlen(leitura));

    mensagem = (char **)malloc(n * n * sizeof(char));

    for (j = 0; j < n; j++){
        for (i = 0; i < n; i++){
            fscanf(arquivo, "%c", &mensagem[i][j]);
        }
    }

    for (j = 0; j < n; j++){
        for (i = 0; i < n; i++){
            if(mensagem[i][j] == '*'){
                printf(" ");
                i = n - 1;
            }
            else {
                printf("%c", mensagem[i][j]);
            }
        }
    }

    fclose(arquivo);
    free(mensagem);

    return 0;
}

Here’s the data in the file:

AEEUMOLSHMSCGT*AGU2A***L:****T*****A

And here’s the expected output:

ALG2: ESTA EH UMA MSG OCULTA

And here’s the logic behind it:

A E E U M O
L S H M S C
G T * A G U
2 A * * * L
: * * * * T
* * * * * A

I do know that the problem is exactly with the call of printf, but I’m unsure on how to fix it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:45:59+00:00Added an answer on June 17, 2026 at 10:45 am

    This is a classic example of “two stars does not a two-dimensional array make”.

    mensagem = (char **)malloc(n * n * sizeof(char));

    for (j = 0; j < n; j++){
        for (i = 0; i < n; i++){
            fscanf(arquivo, "%c", &mensagem[i][j]);
        }
    }
    

    mensagem is a pointer to a block of n * n bytes. However, when you do mensagem[i][j], the compiler will read the value of the pointer, add i, and then read the pointer at that address. What is that pointer set to? Whatever happens to be at i * sizeof(char *) into your allocated memory – probably zero.

    You need to do a two-stage allocation, first allocating n pointers, then allocating storage for each string of n bytes.

    Edit: I intentionally haven’t written the code to solve your problem, because you will leard much more from writing it yourself…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I have fixed all but two warnings now, so thank you all for
The question explains it pretty fully, but here's some more detail: I have a
EDIT I have finally figured out the problem i have been having on my
EDIT: I have edited my post... Working on a project (c#), I have a
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i
EDIT: I have realized the source of my problem. I only have count information
EDIT: I have changed the question as it was too localized and gained negative
I won't want to have edit any working sets. I just want a way
I have an edit text which functions as a search box in my application.
I have two Edit Fields Username and Password and everytime i revert back to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.