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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:53:19+00:00 2026-05-22T11:53:19+00:00

This code is driving me up the wall. The goal is to split to

  • 0

This code is driving me up the wall. The goal is to split to char[] based on a comma. It works in java. But prints weird output in C. The error I suspect is at 28 on the second iteration of the loop where I tried to add 5 the array turns into weird characters.

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

main()
{
    char asshat[] = {'4','5','5',',','7','4','7','\0'};

    int firstSize = 0;//
    int secondSize = 0;//

    //new = 4 \0 \0
    char first[] = {'0', '\0'};//
    //new = 
    char second[] = {'0', '\0'};//
    char *first_ptr = first;
    char *second_ptr = second;

    int takingFirst = 1;
    int takingSecond = 0;

    int i;
    for (i = 0; i < strlen(asshat); i++)
    {
        if (asshat[i] != ',')
        {
            if (takingFirst == 1)
            {
                first_ptr[firstSize] = asshat[i];//ERROR here when you add 5 you s**t bricks
                firstSize++;
                if (asshat[i+1] != ',')
                {
                    char new[firstSize+2];
                    int k;
                    for (k = 0; k < strlen(first_ptr); k++)
                    {
                        new[k] = first_ptr[k];
                    }
                    new[firstSize] = '0';
                    new[firstSize+1] = '\0';
                    first_ptr = new;
                }
            }
            if (takingSecond == 1)
            {
                second_ptr[secondSize] = asshat[i];
                secondSize++;
                if (asshat[i+1] != '\0')
                {
                    char new[secondSize+2];
                    int k;
                    for (k = 0; k < strlen(second_ptr); k++)
                    {
                        new[k] = second_ptr[k];
                    }
                    new[secondSize+1] = '\0';
                    second_ptr = new;
                }
            }
        }
        else
        {
            takingFirst = 0;
            takingSecond = 1;
        }
    }
    printf("%d\n",strlen(first_ptr));
    printf("%c%c%c\n",first_ptr[0],first_ptr[1],first_ptr[2]);
    printf("%s\n",first_ptr);

    printf("%d\n",strlen(second_ptr));
    printf("%c%c%c\n",second_ptr[0],second_ptr[1],second_ptr[2]);
    printf("%s\n",second_ptr);
}
  • 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-22T11:53:20+00:00Added an answer on May 22, 2026 at 11:53 am

    It’s very hard to read what your solution is doing. I tried reimplementing it to be simpler while maintaining your pointer play:

    #include <stdio.h>
    
    int 
    main(int argc, char *argv[])
    {   
      /* Keeping variable names consistent :) */
      char asshat[] = "456,747";
      char *first = asshat, *second = asshat, *c = asshat;
    
      while (*c) {
        if (*c == ',') {
          *c = '\0'; second = ++c;
        } else c++;
      }   
      printf("first: %s\nsecond: %s\n", first, second);
      return 0;
    }   
    

    This produces:

    first: 456
    second: 747
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very beginner question, but driving me crazy. Why isn't this code
This is driving me nuts. I am using some 3rd-party code in a Windows
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This is driving me crazy: I have function void gst_init(int *argc, char **argv[]); in
this may seem like a overly complicated question, but it has me driving me
Ok so this is driving me mad. how can this code possibly not work?
So this is driving me crazy. Basically, when I hard-code my user name and
this piece of code is driving me crazy. I'm trying to print a help

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.