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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:47:26+00:00 2026-05-25T13:47:26+00:00

As you can see below I have created a little program to concatenate 2

  • 0

As you can see below I have created a little program to concatenate 2 strings using C, as you may imagine this code doesn’t work, I have already corrected it myself by using Array notation instead of pointers, and it works just fine, however I’m still not sure why is it that my code fails being almost a replica of my corrected code.

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

void concatena(char *str1, char *str2){
 char *strAux;
 int mover;
 mover = 0;
 strAux = (char *)(malloc(strlen(str1) + strlen(str2)+2));
 *(strAux) = '\0';
 if(str1 == '\0')
    *strAux = '\0';
 else
     while(str1 != '\0'){
         *(strAux+mover++)=*(str1++);
     }
 if(str2 == '\0')
         *strAux = '\0';
 else
     while(str2 != '\0'){
         *(strAux+mover++)=*(str2++);
     }
 strAux='\0';
 str1=strAux;
 printf("%s", str1);
 free(strAux);
 }

I´m still a C beginner (And yes, I’m aware that there are libraries like string.h, I’m asking this for academic reasons) and I have been told that char pointers and arrays are the same thing, something that confuses the heck out of me.

Any help is greatly appreciated.

  • 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-25T13:47:26+00:00Added an answer on May 25, 2026 at 1:47 pm

    The first problem I see is with this section:

    if(str2 == '\0')
        *strAux = '\0';
    

    Just before this code, you’ve filled up strAux with the string from str1.
    Then, if str2 is empty, you suddenly put a null-terminator at the beginning of strAux, eliminating all the work you’ve done so far!

    I think what you intend is:

    if(*str2 == '\0')
         *(strAux+mover) = '\0';
    

    Its the same thing again after your loop for str2, you have the code:

    strAux='\0';
    

    Again, this puts a null-terminator at the start of strAux, effectively ending the newly created string before it even gets started.


    Here’s how I’d re-write your code:

    void concatena(char *str1, char *str2){
        char *strAux;
        int mover = 0;
    
        strAux = (char *)(malloc(strlen(str1) + strlen(str2)+1));  // Changed to +1, NOT +2
        *(strAux) = '\0';   // Start the string as (empty)
    
        while(*str1 != '\0'){   // Copy the first string over.
             *(strAux+mover++)=*(str1++);
        }
    
        while(*str2 != '\0'){   // Copy the second string over.
             *(strAux+mover++)=*(str2++);
        }
    
        *(strAux+mover)='\0';  // End the new, combined string.
    
        printf("%s", strAux);  // Show the results.
        free(strAux);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below. As you can see I am passing two variables
As you can see below, I have Parallel.For loop. If run the program while
I have a SOAP message (see below). Using Xpath, how can I extract the
I have some really funky code. As you can see from the code below
I have a table defined (see code snippet below). How can I add a
I'm having an issue with a TCPListener. I have created this code below and
Trying not to lose it here. As you can see below I have assigned
I have two async classes in my application which you can see below class
i have a pretty complicated form. as you can see below: alt text http://img9.imageshack.us/img9/2465/test2xk.jpg
I have one text input and one button (see below). How can I use

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.