in Education by
I have an array in C, let's suppose it's **arr = malloc(100 * sizeof(char*)); through some function, I populate this array in such a way that it ends up being arr = {"first", "second", "third", "fourth", ... "tenth"}; Now, if I want to populate it anew, so that it's arr = {"1st", "2nd", "3rd"}; //Desired outcome it instead is shown as arr = {"1st", "2nd", "3rd", "fourth", "fifth", ... "tenth"}; //Actual outcome I'm certain my code is wrong, and my function for populating it from empty is fine, but I just want to find a way to wipe it. I tried free(), but I either used it wrong or that's not what I should be using. Do any of you have a function that can not just set a string to "\0", but completely eradicate it? To all that commented and answered, thank you. What I was doing was using text files, strtok, and strdup to read words from a file into a char double pointer. I don't want any more than 5000 words stored in the "array", even if my file is 5001+ words. I did this. I successfully populated the memory with the strings from the file. But if I changed my mind and tried to populate the memory with a smaller file (~30 words), it would change the first 30 and leave the remaining 4970 as they were, when I wanted them not to exist. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use realloc function to change capacity of your arr variable:arr = realloc(arr, 3 * sizeof(char*)) - reallocate memory for 3 elements. Then you should assign these 3 elements to your arr

Related questions

0 votes
    I have an array in C, let's suppose it's **arr = malloc(100 * sizeof(char*)); through ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Parallel processing is done via __________ package can make the elapsed time smaller than the user time. (a) ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    A gas at 350 K and 15 bar has molar volume 20 percent smaller than that for an ideal gas under the ... attractive forces are dominant Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    Rithwika wants to help her visually challenged elderly uncle to maintain the room temperature by switching on AC; ... help ASAP Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Why does an FBW system use a smaller tail area? 1) Bigger tails are harder to move as they produce larger wing ... To reduce weight and drag but at a cost of natural stability...
asked Dec 25, 2020 in Technology by JackTerrance
0 votes
    import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Thinking like a simple array: Console.WriteLine("Number: "); int x = Convert.ToInt32(Console.ReadLine()) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Which of the following correctly declares an array in C++? a) array{10}; b) array array[10]; c) int array; d) int array[10];...
asked Oct 19, 2022 in Education by JackTerrance
0 votes
    Which is the correct way to create an array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python"]; III) ... 1. I, III 2. II, III 3. I, II 4. I, II & III...
asked Feb 24, 2021 in Technology by JackTerrance
+1 vote
    Define an array in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Through ChatOps, communication can be bi-directional, that is, users can submit commands from the chat room to the bots, ... status to the chat room directly. A. True B. False...
asked Dec 23, 2022 in Technology by JackTerrance
...