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 6914009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:17:34+00:00 2026-05-27T09:17:34+00:00

I am trying to normalize a C string that contains a number so that

  • 0

I am trying to normalize a C string that contains a number so that additional zeros are added in the beginning make the whole string contain MAXCOL characters (which is 8 ..)

Everything seems to be working fine up to the point where i add the original string back into the normalized string : Some random characters are being added idk how, it’s like i am adding characters that are after the \0 character, making the string look weird when shown on screen.

I hope someone has a clue as to what i am doing wrong.

To give you an idea, t_operande2D is a 2D array of char[MAXCOL]

void normaliser_operandes(t_operande2D mes_operandes){
    // This procedure adds zeros to the beginning of each mes_operandes to have a normalized nbr of MAXCOL chars
    // 
    // Lets say it receives mes_operandes[0] as 10, it will convert it to 00000010

    int j, nombre_caracteres; // Nbr of original caracters
    t_operande2D operandes_normalises; // The normalized output (temp variable)

    for(int i=0; i<MAXLIGNE; i++){
        // Count the original nbr of chars
        nombre_caracteres = 0;
        for(int j=0; (j<MAXCOL && mes_operandes[i][j]!='\0'); j++){
            nombre_caracteres++;
        }
        // Add needed 0s to the beginning
        for(j=0; j< MAXCOL - nombre_caracteres; j++){
            operandes_normalises[i][j] = '0';
        }
        operandes_normalises[i][j] = '\0';

    strncat(operandes_normalises[i], mes_operandes[i], nombre_caracteres);

    strncpy(mes_operandes[i], operandes_normalises[i], MAXCOL);
    }
}

EDIT: after the edit, the strncat is giving me the “stack around operandes_normalises was corrupted…” error 🙁

  • 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-05-27T09:17:34+00:00Added an answer on May 27, 2026 at 9:17 am

    strcat expects both of it’s arguments to be NUL terminated. By the looks of things, operandes_normalises[i] begins with MAXCOL - nombre_caracteres amount of '0' chars, but isn’t followed up by a NUL terminator ('\0') to indicate the end of the string.

    strcat will look for the end of the string before appending mes_operandes[i] to operandes_normalises[i]. If the junk characters are between the '0'‘s and mes_operandes[i], then that’s your problem – operandes_normalises[i] is missing a terminator.

    If the junk characters are after mes_operandes[i], then mes_operandes[i] is the one that isn’t NUL terminated.

    Print out operandes_normalises[i] after adding the '0'‘s to the start of the string to see if this is the case. By the looks of things I’m guessing there’s a good chance it is.

    ie. If MAXCOL - nombre_caracteres is 5, operandes_normalises[i] could be:

    "00000random_garble\0" /* just so happens to be a '\0' somewhere in memory */
    

    Therefore mes_operandes[i] will be added after the junk characters.

    EDIT: Thought I might add, strcat is unsafe, especially in the way it’s being used here – no checking to ensure that mes_operandes[i] will fit in operandes_normalises[i].

    Consider using strncat, however note that it writes n + 1 characters to the destination. From the manpages:

    If src contains n or more characters, strncat() writes n+1 characters to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.

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

Sidebar

Related Questions

I'm trying to normalize a mysql database.... I currently have a table that contains
Trying to make a make generic select control that I can dynamically add elements
I'm trying to normalize arbitrary chains of .Skip() and .Take() calls to a single
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a MySQL-based application support MS SQL, I ran into the following
I was trying to normalize a set of numbers from -100 to 0 to
I'm trying to normalize a big amount of Affymetrix CEL files using R. However,
I have a few images I'm trying to normalize to a certain scale by
I'm trying to build a search engine that goes through online vehicle classifieds such
I am trying to make a custom light shader and was trying a lot

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.