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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:01:01+00:00 2026-06-14T00:01:01+00:00

I have tried to write a short program in C that takes in space

  • 0

I have tried to write a short program in C that takes in space separated tokens from stdin, shuffles (permutes) the tokens and then prints the shuffles tokens to stdout. My shuffling algorithm works fine, the problem lies in the token parsing. I wanted to store the tokens in a dynamic string array so the program could support any number of tokens to be passed in through stdin. However, my current implementation of the dynamic array results in a segmentation fault whenever the array needs to be expanded. What am I doing incorrectly?

My Code:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 33

int main(int argc, char* argv[]) {
  int i,j,k;
  i=j=k=0;
  int arrSize = 10;
  /* Temp fix */
  /* Get token count as command line argument */
  //if (argc==2) { sscanf(argv[1],"%d",&arrSize); }
  char  c='\0';
  char* t;
  char** datArr = (char**)malloc(sizeof(char*)*arrSize);
  //char* datArr[arrSize];
  char  token[BUFFERSIZE];
  while ( (c=getc(stdin)) != EOF && c != '\0') {
    if(isspace(c)) {
      token[i] = '\0';
      i=0;
      if ( j >= arrSize) {
        arrSize *= 2;
        realloc(datArr, arrSize);
      }
      char* s = (char*)malloc(sizeof(char[BUFFERSIZE]));
      strcpy(s,token);
      datArr[j++] = s;
    }
    else if(i+1 < BUFFERSIZE) {
      token[i++] = c;
    }
  }
  /* Permutate & Print */
  srand(time(NULL));
  for(i=0;i<j;++i) {
    k = rand()%(j-i);
    t = datArr[k];
    datArr[k] = datArr[j-i-1];
    datArr[j-i-1] = t;
  }
  for(i=0;i<j;++i) { printf("%s ",datArr[i]); }
  printf("\n");
  return 0;
}

NOTE: I know I did free the memory yet

Some sample input (to keep it card themed):

2C 3C 4C 5C 6C 7C 8C 9C 10C JC KC QC AC 2S 3S 4S 5S 6S 7S 8S 9S 10S JS KS QS AS
  • 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-14T00:01:02+00:00Added an answer on June 14, 2026 at 12:01 am

    VaughnCato’s comment on the original OP was the correct answer to the question.

    “You need to use datArr = (char**)realloc(datArr,arrSize*sizeof(char*));“

    Thanks VaughnCato!

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

Sidebar

Related Questions

I have tried to write my first Boost program from information on the Boost
I have to write a program that will essentially sort nodes in a linked
I have written a short String reverse program in C++. I decided to write
I have a python program in an embedded system that needs to write to
How can you write a VB.NET Windows Forms Application via CodeDom? I have tried
I have already tried multiple ways. defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ORGANIZATIONNAME=YourNameHere;}' Also i've tried
Long story short: I'm trying to write an app that'll dump IE's history to
I have a CUDA application that is data movement bound (i.e. large memcopies from
I've found similar questions but never the exact answer. I have Qt program that
I've been trying to write a program in R that implements Newton's method. I've

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.