C :: Transferring Contents Of One Character Array To Another Function

Feb 1, 2014

In the function *expandArrayList and *trimArrayList I need to allocate either more memory or less memory to the given array. Right now I am using malloc on a new array and then transferring the elements of the original array to the new array, freeing the original array, and then reassigning it to the new array so it's size and capacity are either bigger or smaller than the original. I feel like there has to be a simpler way to do this with either realloc or calloc, but I cant find sufficient information on either online. Would I be able to use either to make this easier and the code cleaner?

Also, these are character arrays that contain several character arrays inside them, so I was wondering what the best way to transfer the contents of one array to the other would be. I have gone between

Code:

//example
for (i=0; i<length; i++){
newarray[i] = oldarray[i];
}
and
Code: for (i=0; i<length; i++){
strcpy(newarray[i], oldarray[i]);
}

but I'm not sure which one (if either) should work.

The 'ArrayList.h' file that is included contains the structure ArrayList, as well as the function prototypes for the functions listed below.

Here is the structure in ArrayList.h:

Code:

#define DEFAULT_INIT_LEN 10
typedef struct ArrayList {
// We will store an array of strings (i.e., an array of char arrays)
char **array;

[Code] ....

Here is my code:

Code:

//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
//Create arraylist

[Code]....

View 3 Replies


ADVERTISEMENT

C/C++ :: How To Open A File And Store Contents Into Character Array

Jan 2, 2014

I am trying to store a large text file into an array so I can count the number of characters.

View 2 Replies View Related

C++ :: Modify Contents Of 1D Character Array - Converting To Upper Case

Feb 10, 2015

I am unsure how to write a function which modifies the content of the 1D character array and puts all of the letter it contains into uppercase. the following are the letters which i am trying to convert.

char text[MAX+1] = {'T', 'e', 's', 't', 'e', 'r', EOT};

The output to this should look like T E S T E R EOT

View 2 Replies View Related

C++ :: Char Isn't Transferring In Function

May 14, 2014

In my classification function, the char isn't transferring to main. Am i suppose to make it a string. Or perhaps something else i messed up on

#include <iostream>
using namespace std;
double validity(double, double, double);
double classification(double, double, double, char);

[Code]....

View 10 Replies View Related

C++ :: How To Put File Data Into Members Of A Class - Array Transferring

Mar 10, 2013

I'm trying to put file data into members of a class. Remember to type in the file name you want to open. Cool feature right? I just had Dbase.txt so I chose that.

Fixed stuff in the .txt. Now I need to figure out why it only does 1 set and then ends.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class INFO {

[Code] .....

Dbase.txt:
Bob
Guy
Programmer
M
9999.99
40
------------------
Little
Guy
Little Brother
M
0.0
3
------------------

View 14 Replies View Related

C++ :: Function That Subtract Value From Each Character In Array

Mar 17, 2013

I have to write a function Subtracts val from each character in the array

// If the array is: abcdefghi and value = 5, then result is: 5 ascii characters subtracted from the given array
// note: Make sure the character remains in the ' '
// to '~' (inclusive)
// If character < ' ', then add 95
// If character > 126, then subtract 95
// to put back within the range, and repeat as needed
// all printable characters are between this range
// ' ' is the 1st printable character (32)
// '~' is the last printable character (126)
note: subtracting values from the char will cause an overflow.

the prototype is: void subtracting(char array[], int size, int value)

View 3 Replies View Related

C :: Trying To Fill A Character Array With Scanf Function

Oct 5, 2014

I'm trying to make a character array, and be able to custom fill it by using the scanf() function to ask the user for each character spot in the array.

Code:
#include <stdio.h>
int main() {
int i;
char character_array[11];
char *charpointer;
charpointer = character_array;

[Code] .....

And this is the output i get when i run it:

Code:
root@kali-Tulips:~# ./myown
what letter would you like in the [0] spot of the array? :
a
what letter would you like in the [1] spot of the array? :
what letter would you like in the [2] spot of the array? :
b
what letter would you like in the [3] spot of the array? :
what letter would you like in the [4] spot of the array? :

[Code] .....

Obviously I'm trying to grasp C programming and am very new. I don't understand why this isn't working, and i think its more than my lack of knowledge of C syntax. I believe the correct memory is allocated but when examined with gdb i don't find what i expect....

View 4 Replies View Related

C :: Function Which Takes A Character Array And Reverses It

Dec 7, 2013

Write a c function which takes a string(as a character array, null terminated) and reverses it. it doesn't print the reversed string -- it modifies the given string so that it is reversed.

Code:

void reverse(char array[]) {
char tmp;
int i, j;
for(i=0;i<strlen(array); i++){
for(j=strlen(array)-1;j>=0;j--) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
} } }

how come this doesn't work?

View 2 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C :: Printing Contents Of A File - Prints One Extra Character Not Present In The File

Feb 12, 2013

I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)

_______lastname_______firstname__90__________lname __________fname_100___________last___________first __60

When printed out, the names are formatted as follows:

lastname, firstname: 90
lname, fname: 100
last, first: 60

However, when I print them out this is what I get:

lastname, firstname: 90
lname, fname: 100$
last, first: 60H

For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.

Code: /*
- Prints a single record stored in the file pointed to by ifp.
*/
void print_record(FILE *ifp, int record) {

[Code]......

View 6 Replies View Related

C :: Assignment Of Element Of 2D Character Array To 1D Character Array

Jul 4, 2014

Can we do this :

Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
temp=strings[1];

View 3 Replies View Related

C/C++ :: Vector Contents Unexpectedly Different Within The Same Function

Jun 14, 2014

I am trying to create a tennis tournament simulator. There are 128 players in the tournament, and 32 seeds. I have a vector filled with 'Player' objects named 'Players'. Each 'Player' object has three member variables, Strength, Ranking, and ATPPoints. When the vector 'Players' is first declared, the default constructor for each 'Player' object is used, which initialises each of the 'Player' objects to zero. I then generate a Strength value in the range of 0-10 for each of these 'Player' objects, and sort them descendingly according to their strength.

I now want to create the draw for a tournament. I declare a vector 'Draw' with 128 'slots'. Each slot initially contains a blank 'Player' object, with its member variables set to 0. I first place the top 32 players throughout the draw such that they do not meet until later rounds as they would be in real life. For the remaining players, I position them in the draw by generating a random number between 0 and 127. Some of the slots 0-127 are occupied by seeded Players. These seeded Players have non zero Strength and Rank values, so to avoid overwriting these slots, I check the Strength value of the 'Player' object in the slot in question. A return value of 0 would indicate an empty slot.

At the beginning of the function, I output the Strength and Rank values of the 'Player' objects already in the draw. This outputs correctly.

for (Player& p : Draw)
{// outputs the draw in it's current state, with the already placed seeds as expected.
cout << p.GetRanking() << " " << p.GetStrength() << endl;
}

[Code]....

View 7 Replies View Related

C :: Transferring Data From One File To Another

Feb 11, 2013

I have some problem with the ssize_t sendfile function.

ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

The error:

Lesson_4_1A_Home_work.c:84:24: error: expected declaration specifiers or ‘...’ before string constant
Lesson_4_1A_Home_work.c:84:37: error: expected declaration specifiers or ‘...’ before string constant
Lesson_4_1A_Home_work.c:84:63: error: expected declaration specifiers or ‘...’ before ‘strlen’

Code:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/sendfile.h>
#include <sys/types.h>

[Code]....

View 3 Replies View Related

C# :: Transferring Information Of Variables

Jun 13, 2014

I am trying to transfer information of Variables. How to get this to work and everything I follow shows that this SHOULD work but some reason does NOT work... I

What I CAN get to work is if I put frm.show(); on one specific section that ONE section will show up... so I tried putting all of the variables at the end button to get them to show up with the one Frm.show and it still only does the very last one and the rest are just blank I dont really understand as to why.

Also if I put a FRM.show at each step it will just keep creating a new instance of form 2 each with a different line that is changed.

Form1 code

public Form1() {
InitializeComponent();
} public void Enter_Click(object sender, EventArgs e) {
Form2 frm = new Form2();
Employee FirstName = new Employee();

[Code] .....

Form2 Code

public partial class Form2 : Form {
public Form1 frm1;
public Form2() {
InitializeComponent();
}

View 8 Replies View Related

C++ :: Function To Replace A File With The Contents Of Another Stream

Apr 26, 2012

I have function which will replace (or create) an file with the contents of another stream. The stream could be anything. The replacement is done safely.

Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
int do_replace(const char *file, int stream, int cnt) {

[Code] .....

View 14 Replies View Related

C/C++ :: Printing The Contents Of The Array?

Feb 18, 2014

I am writing a code with C where I will give an integer and get the binary conversion of it.I tried to fill the binary digits into an integer array.But when I normally print it will give the proper output.But when I try to print the contents of the array it will not produce the proper result.

My code would be as follows.

#include <stdio.h>
#include <conio.h>
int main()

[Code]....

View 5 Replies View Related

C/C++ :: Reversing Array Contents

Feb 21, 2015

I'm trying to reverse 25 items that are in my array by using a For Loop and sending 2 of the numbers to a function at a time. The code I have right here, is not working at the moment, I cannot find the problem because i've been staring at my code for too long.

int farEnd = 24;
for (int i = 0; i < 24; i++){
Swap(intArray[i], intArray[farEnd]);
farEnd--;
}

Also here is the code in my function

void Swap(int num1, int num2){
int temp = 0;

temp = num1;
num1 = num2;
num2 = temp;
}

View 5 Replies View Related

C# :: Transferring Data To Textboxes From Database?

Feb 22, 2015

I can not do transferring data to textboxes from database.

="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1{

[code]....

I got the error this line:

read = cmd.ExecuteReader();

View 3 Replies View Related

C++ :: Display Contents Of Two-Dimensional Array

May 4, 2014

//Introductory20.cpp - displays the contents of a two-dimensional array, column by column and row by row

#include <iostream>
using namespace std;
int main() {
int nums[2][4] = {{17, 24, 86, 35},
{23, 36, 10, 12}};

[Code] .....

I need modifying a program that should display the contents of the two- dimensional array, column by column and also row by row. I need to complete the program using a WHILE statement in the outer loops and a FOR statement in the nested loops.

View 1 Replies View Related

C++ :: Zero In List Contents And Multiset Contents

Apr 24, 2015

HTML Code:

So I insert values from a vector into a list and into a multiset, and I noticed zero is added to their contents! I had to do a whole lot of debugging to find out where the error was, how can i stop this thing? Code which generates such error...

infact i checked the content of vector ups to be sure that there was no zero in it, but after loading into list combi_t * head, it seems like there was a zero added and this is giving me errors when i call function master_roller...

Code:
void ins(combi_t * &testa, int &numero, int &num, int &no)
{ // if (ricerca(testa, numero) == 0)
//{
combi_t *temp = new combi_t;

temp->val0 = numero;
temp->val1 = num;
temp->val2 = no;
temp->nextPtr = 0;

[Code] ....

View 2 Replies View Related

C++ :: How To Load Contents Of Text File Into Array

May 12, 2013

So I am working on a FUSE filesystem and I currently have the need to load the contents of a text file into an array.

My array initialization looks like: char array[max_entries][PATH_MAX] = {NULL}

The reason I want to pass it by reference is that I want my function to return two values essentially. One a specific char* and the other an array like I initialized. My function proto type looks like:

char* load_meta(char* list[max_entries][PATH_MAX], char* path, int mode);

How I'm trying to call the function:

someChar = load_meta(&array, path_name, 1);

Within the function I try to edit the array by deferenceing it once, like this:

strcpy(*list[i], file_entry); // This seg faults

View 8 Replies View Related

Visual C++ :: Resetting Contents Of Existing Array To 0?

Apr 13, 2015

Is there some quick way of resetting the contents of an existing array to 0? Just to be clear, I'm not initializing the array, it already exists, has content and needs to be reset at 0. Is there a faster way than the code below?

Code:
for(i=0;i<100;i++)myarray[i]=0;

View 6 Replies View Related

Visual C++ :: Transferring Unicode String From One Program To Another With UTF-8 Encoding?

Mar 20, 2015

I'm transferring a unicode string from one program to another with UTF-8 encoding.

Program that is sending:

Code:
// Convert path
std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_converter;
CString arg = L" /PATH="" + CString(utf8_converter.to_bytes(path).c_str()) + L""";

Program that is retrieving:

Code:
// Restore original path
std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_converter;
std::wstring path = utf8_converter.from_bytes( argument );

Everything has worked fine, until running on a Japanese edition of Windows.

The "byte path" then looks something like "C:¥Users¥d✝?✝a ,?¥AppData¥Local¥Temp¥file.txt".

"from_bytes()" will throw an std::range_error exception "bad conversion".

The program works fine when working with Japenese writing inside paths in the English edition.

What could be causing the "bad conversion"?

View 10 Replies View Related

C++ :: Read A Text File And Store Contents Into 2D Array?

Aug 2, 2014

read a text file and store the file contents into a 2D array?

100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131

Here's my code:

const int ROWS = 5;
const int COLS = 6;
int array[ROWS][COLS];
ifstream inputFile;
inputFile.open("table.txt");

[code]....

When i run the program and try and display the array, it doesn't work.

View 1 Replies View Related

C++ :: Store User Input Then Display Array Contents?

Jan 10, 2015

I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.

I'm trying to store multiple characters of course, and then display all characters stored.

char guess[27]={0};
cin >> guess[26];
int hit=0;
for(int i=0; i<len; i++) {
if( guess[26] == hidden_word[i] ) {
hit++;
select_word[i] = guess[26];
if(strcmp(hidden_word,select_word) == 0) {

[code]....

EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.

View 6 Replies View Related

C++ :: Store Contents Of Text File Into Char Array?

Apr 22, 2014

I am trying to store the contents of a text file into a char array. However the function i am using ifstream member function get(); seems to stop working when fed with certain characters. Is there another solution besides the get() function that will accept all types of characters from files?

char text[1000];
for (int i = 0; i <= textlen; ++i)
{
text[i] = text_in.get();
}

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved