Thread: 2D Char Array and strcpy/strcat error

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Question 2D Char Array and strcpy/strcat error

    Hi guys,I am stumped on this one. I have a function that prints out a report body. I need to concatenate the last name with first name. Everything was working just fine when I had all of the code in my Main function. When I seperated it out into its own function I get garbage, which I suspect is either a memory pointer or uninitialized data.Here is the code that worked when it was in Main:#define REPORTA " %-14s%5.2f%10.2f%10.2f%8.2f%8.2f%\n"// 3.7 Create Report Body for( curEmp = 1; curEmp

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    I'm still getting used to this forum so please excuse my errors. Here is my code (hopefully).[code]for( curEmp = 1; curEmp

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    This board seems to be ignoring my [code] commands and it kills my post as soon as it reaches a greater-than sign.I am a failure at posting apparently.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First of all, what does your "2D" array look like?

    char twod[100][200];
    Is a 2D array.

    If you have something involving *'s, then it's no longer a 2D array, but something with "pointers".

    Next, how did you pass this to your function, and how is your function declared.
    Chances are, you made a mess of this, ignored some warnings, and then it blew up.

    Code:
    void foo ( char arr[][200] ) {
        strcpy( arr[0], "This is a string" );
    }
    
    int main ( ) {
        char twod[100][200];
        foo( twod );
    }
    Edit:
    For posting code, make sure it's "paste as text", and not some markup HTML/RTF/IDE specific mish-mash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Here is a cut-n-paste from Notepad:
    Code:
    // 3.7 Create Report Bodyvoid createBody(FILE * reportFile, char fName[][NMLEN], char lName[][NMLEN], float empData[][9], int empNum){	for(int curEmp = 1; curEmp < empNum; curEmp++)	{		char fullName[27+1];		strcpy(fullName, lName[curEmp]);		strcat(fullName, ", ");		strcat(fullName, fName[curEmp]);		printf(REPORTA, fullName, empData[curEmp][RATE], empData[curEmp][REGHOUR], empData[curEmp][FED], empData[curEmp][SSI], empData[curEmp][NET]);         }}

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    I just switched to Chrome and what a world of difference. Looks like IE 8 here at work is gimped.

    Here is the code again (crosses fingers):

    Non-Working:
    Code:
    #define REPORTA "  %-14s%5.2f%10.2f%10.2f%8.2f%8.2f%\n"
    char fName[EMPNUM][NMLEN],lName[EMPNUM][NMLEN];
    
    // 3.7 Create Report Body
    void createBody(FILE * reportFile, char fName[][NMLEN], char lName[][NMLEN], float empData[][9], int empNum)
    {
        for(int curEmp = 1; curEmp < empNum; curEmp++)
        {
            char fullName[27+1];
            strcpy(fullName, lName[curEmp]);
            strcat(fullName, ", ");
            strcat(fullName, fName[curEmp]);
            printf(REPORTA, fullName, empData[curEmp][RATE], empData[curEmp][REGHOUR], empData[curEmp][FED], empData[curEmp][SSI], empData[curEmp][NET]);
            }
    }
    Working but in Main:
    Code:
    char fName[EMPNUM][NMLEN],lName[EMPNUM][NMLEN];
    
    // 3.7 Create Report Body
        for( curEmp = 1; curEmp <= EMPNUM; curEmp++)
        {
            strcpy(fullName, lName[curEmp]);
            strcat(fullName, ", ");
            strcat(fullName, fName[curEmp]);
          printf(REPORTA, fullName, payRate[curEmp], regHours[curEmp], fedTax[curEmp], ssiTax[curEmp], netPay[curEmp]);
             }
    Last edited by SactoDoug; 04-25-2012 at 10:46 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char fullName[27+1];
    So how does this relate to NMLEN ?
    It's wrong because the length should be an even number + 1, given what you're copying into it.

    What about
    char fullName[(NMLEN)*2+2+1];


    > Working but in Main:
    ...
    > for( curEmp = 1; curEmp <= EMPNUM; curEmp++)
    Which is odd, because this is an array overrun.

    Subscripts run from 0 to N-1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Thank you for the reply.

    I fixed the fullName buffer's size. I also went over my code again and found a couple of memory overruns. I also found where I assumed that a float array that I created locally was initiated at zero but it really was not. I initiated it properly. Now everything works.

    Thank you.


    Quote Originally Posted by Salem View Post
    > char fullName[27+1];
    So how does this relate to NMLEN ?
    It's wrong because the length should be an even number + 1, given what you're copying into it.

    What about
    char fullName[(NMLEN)*2+2+1];


    > Working but in Main:
    ...
    > for( curEmp = 1; curEmp <= EMPNUM; curEmp++)
    Which is odd, because this is an array overrun.

    Subscripts run from 0 to N-1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error with strcpy and strcat
    By KMAN999 in forum C Programming
    Replies: 2
    Last Post: 07-27-2011, 09:39 AM
  2. strcpy() and strcat()
    By Moony in forum C Programming
    Replies: 5
    Last Post: 07-03-2006, 01:18 AM
  3. char array/strcat() problem
    By yahn in forum C++ Programming
    Replies: 12
    Last Post: 02-18-2006, 05:26 PM
  4. strcat - strcpy
    By pizzas in forum C Programming
    Replies: 4
    Last Post: 08-05-2003, 02:11 AM
  5. strcat or strcpy?
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 12:10 PM