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

  • Home
  • SEARCH
  • 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 8055871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:31:57+00:00 2026-06-05T08:31:57+00:00

I am working on my first C program, which needs to construct and deconstruct

  • 0

I am working on my first C program, which needs to construct and deconstruct strings as it pleases. I don’t know how to do that correctly, though. Could someone please explain?

Here’s my code as far as I get it:

int main( int argc, char ** argv ) 
{ 
    if ( argc != 2 ) 
    { 
        _print_help(); /* defined above main() */ 
        exit(0); 
    } 

    char *s = argv[1]; 
    char *val = ""; 
    register int i; /* no idea why I need "register" and not just int */
    for(i=0;s[i];++i) /* loop over the string, take each character 
    {                    and add it to another string */
        strcat(val,s[i]); 
        printf("v:%s;\n",val); 
    } 
    printf("expression: %s\n",s); 
}

gcc is called from a make file (generated with autotools):

gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT c-calc.o -MD -MP -MF .deps/c-calc.Tpo -c -o c-calc.o c-calc.c

And now it throws warnings that I shouldn’t put an integer as second input to strcat and also that string3.h expects const char * __restrict__ and not just a char (I have to translate them because my Ubuntu talks to me in German).
Activating my compiled program throws an Memoryaccess error and then the program returns with code 139 which seems to mean Segmentatation violation.

  • 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-05T08:32:00+00:00Added an answer on June 5, 2026 at 8:32 am

    Very good for the first program 😉 First I write a program which does what you want, then I’ll explain it a bit more.

    int main( int argc, char ** argv ) 
    { 
        if ( argc != 2 ) 
        { 
            _print_help(); /* defined above main() */ 
            exit(0); 
        } 
    
        char *first_arg = argv[ 1 ] ;
        char val[ 1024 ]; //You should make enough memory to hold all the strings concatenated to each other. Here we suppose that 1024 is enough.
    
        strcpy( val , "" ) ; //Since val is an accumulator it should be initialized first.
    
        for( int i = 0 ; i < strlen( first_arg ) ; i++ ) /* loop over the string, take each character and add it to another string */
        {
            char char_str[ 2 ] ;
            char_str[ 0 ] = first_arg[ i ] ;
            char_str[ 1 ] = '\0' ;
    
            strcat( val , char_str ) ; 
            printf( "v:%s;\n" , val ) ; 
        }
    
        printf( "expression: %s\n" , val ) ; 
    }
    

    OK let’s explain things better. First of all, you should know what a pointer is. A pointer is just a number which points to a place in the memory (usually heap memory). When you define char *val it is nothing more than a pointer and there is nothing allocated behind it. You have to use following commands to initialize it.

    char val[ 1024 ] ;
    char *val = new char[ 1024 ] ;
    

    These commands are different. Actually the first command allocates the required memory( 1024 bytes) from the stack. But the former gets 1024 bytes from the heap memory. Former does not need to get freed because when you leave its containing block, it’ll get freed. But the second command needs to get released manually by the programmer using this command :

    delete [] val ;
    

    About these lines :

    char char_str[ 2 ] ;
    char_str[ 0 ] = first_arg[ i ] ;
    char_str[ 1 ] = '\0' ;
    

    first_arg is a pointer which points to argv[ 1 ], this means they hold the same address. Characters are different from strings in C/C++. 'A' is different from "A". All strings in C/C++ should end with '\0' character. "A" can be seen as a string with 2 characters which the first is 'A' and the second one is '\0'. As well strcat() method always concatenate 2 strings so I had to build an string. A character with 2 cells which the first if my desired character and the next is a '\0' character. Now I can use this string to add it to val.

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

Sidebar

Related Questions

I am working on a program that needs to determine which JCheckBox was selected.
I don't understand why the first program in JSP is working, but the second
I'm working on a Java program that needs to send an image (preferably in
I am working on a program that needs to do the following: Write a
I'm working on a socket program and everything seems fine when compiling. First I
I am working on my first C# program and have run into a brick
I need to use GMP in an iphone program I'm working on, but don't
I'm working on a project in which I'm supposed to write a C program
I'm working on a large program which has an abstracted GUI API. It is
I'm working on my first real WinForms application, and my boss has asked that

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.