Thread: overlay of strings

  1. #1
    Unregistered
    Guest

    overlay of strings

    can someone tell me why this gives an memory error
    thank you

    #include <malloc.h>
    #include <stdio.h>
    #include <string.h>


    #define MAX_LEN 40

    void main()
    {
    int pos;
    char *line, *aster, *blank;

    line=(char*)malloc(MAX_LEN);
    aster=(char*)malloc(1);
    blank=(char*)malloc(1);

    line=" ";
    aster="*";
    blank="";

    printf("enter star position (1-40):");
    scanf("%d",&pos);

    if (pos>0 && pos<41)
    {
    printf("\n");
    printf(" 1 2 3 4\n");
    printf("1234567890123456789012345678901234567890\n ");
    puts(strcat(strncat(blank,line,pos-1),aster));
    }
    else
    printf("out of range");
    }

  2. #2
    Unregistered
    Guest
    edit

    line=" ";

    this has 40 spaces

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #include <malloc.h>
    The ANSI header file for malloc is stdlib.h

    > void main()
    This must be
    int main ( )

    > line=(char*)malloc(MAX_LEN);
    You don't need the cast (it can hide errors)

    > aster=(char*)malloc(1);
    Given the use of string functions, and that all C strings have a \0, you need at least 2 bytes here.

    > line=" ";
    If you really want to set the memory, then you should use strcpy - this just makes the pointer point at this string constant, and throws your malloc'ed memory away
    strcpy( line, " " );

    > puts(strcat(strncat(blank,line,pos-1),aster));
    Perhaps you should try breaking this up into several smaller steps, so you can more clearly see what is going on.
    In particular
    1. the destination for the copy is blank, which at best is only 1 char long.
    2. strncat doesn't guarantee that a \0 will be placed in the expected position, so the following strcat could be in deep trouble.

  4. #4
    Unregistered
    Guest
    changed to this


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


    #define MAX_LEN 40

    int main()
    {
    int pos;
    char *line, *aster, *blank;

    line=(char*)malloc(MAX_LEN);
    aster=(char*)malloc(2);
    blank=(char*)malloc(2);

    strcpy(line," ");
    aster="*";
    blank="";

    printf("enter star position (1-40):");
    scanf("%d",&pos);

    if (pos>0 && pos<41)
    {
    printf("\n");
    printf(" 1 2 3 4\n");
    printf("1234567890123456789012345678901234567890\n ");
    puts(strcat(strncat(blank,line,pos-1),aster));
    }
    else
    printf("out of range");
    }


    i tried to break that complex bit up before but it did not seem to help.

    i get a windows error saying

    the instruction at "0x004013e4" referenced memory at "0x004230c0". The memory could not be "written".

    this is still what i was getting before.

    its not important as its just an example for a book as i am learning, but i you feel like solving, thanks.

  5. #5
    Registered User argon's Avatar
    Join Date
    Jan 2002
    Posts
    12
    I believe your problem is that you´re trying to strcat a pointer, and you should use an array of characters.

    try this:

    char blank[MAX_NUMBER];

    then you must use strcpy

    strcpy(blank,"");

    hope it works!!
    Greetings from México

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LEN 40
    
    int main() {
        int pos;
        char *line, *aster, *blank;
    
        line=malloc( MAX_LEN+1 );
        aster=malloc( 2 );
        blank=malloc( MAX_LEN+1 );  // you're copying to this - better have space
    
        strcpy( line, "                                        " ); // 40 spaces
        strcpy( aster, "*" );
        strcpy( blank, "" );
    
        printf( "enter star position (1-40):" );
        scanf( "%d",&pos );
    
        if(pos>0 && pos<41) {
            printf( "\n" );
            printf( "         1         2         3         4\n" );
            printf( "1234567890123456789012345678901234567890\n" );
            puts( strcat( strncat( blank,line,pos-1 ),aster ) );
        } else
            printf( "out of range" );
    
        return 0;
    }

  7. #7
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    printf("%*s\n", -pos, "*");

    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM