C :: How To Correctly Use Realloc On Array Of Char Arrays

Mar 21, 2013

how to correctly use realloc on an array of char arrays? Say I want to store strings in arrays (=array of char arrays) and double the size of max. strings (y value):

Code:

int x=200;
int y=10;
char *carray[y];
for (int j = 0; j < y; ++j)
carray [j] = malloc (sizeof(char)*x);}
}

[code]...

fix the realloc part of my code?

View 2 Replies


ADVERTISEMENT

C++ :: Concatenate Two Char Arrays Into Single Char Array?

Sep 29, 2014

I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.

/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit

[Code]....

View 2 Replies View Related

C++ :: Dynamically Realloc 2D Array

Dec 18, 2014

I would like to realloc a 2D array. I have a counter, itime, it increases each step. Each step, I would like to reallocate my array, keeping the old values, and add new values to the array. When itime=1, I use only malloc, because it is allocated for the first time. When itime increases (e.q. itime=2), realloc comes into process. In the realloc process the GUI crashes.

int itime;
char ** solpointer;
itime = 1;
do {
if( itime == 1 ) {
solpointer = (char**)malloc(sizeof(char*) * itime);
solpointer[itime-1] = (char*)malloc(sizeof(char) * 32);

[code]....

View 3 Replies View Related

C++ :: Constant Char Not Being Interpreted Correctly

Oct 23, 2013

I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:

bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)

Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().

If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.

Following is my code. MyCompany::company is a QString.

const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);

View 3 Replies View Related

Visual C++ :: Can't Make Maze Work Correctly With Arrays

Apr 30, 2014

I have to make a maze with arrays, but i cant seem to move down or up or at all. Here is what I have so far

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

[Code].....

View 5 Replies View Related

C# :: Not Loading Dynamic Array (128,8) Correctly

Feb 14, 2013

Code:
public void dam_data_setup() {
// fill list
damgtype.Add( den1);
damgtype.Add( den2);
damgtype.Add( da1);
damgtype.Add( da2);
damgtype.Add( db1);
damgtype.Add( db2);

[Code] .....

This is a genetics program and is to parse the source array and write all possible combinations to a new array. All sections but dilute work correctly. For some reason the dilute's Boolean is not testing true when it should. This is causing data corruption.

View 3 Replies View Related

C :: Simple Use Of Malloc And Realloc

Jan 27, 2013

i want to improve my knowledge about the dyn allocation of char pointers... with this code i wanted to type a string and insert the string in a array created dynamically:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char c;
char *test=NULL;
unsigned int len;
}

[code]....

why there are these 3 initial character '' ')' ':' that i didn't have typed...

View 4 Replies View Related

C :: Use Realloc To Extend The Memory?

Feb 24, 2013

I need to use realloc to extend the memory when it goes out of bounds but I am not getting a correct output.

this is how i am trying to realloc

Code:

if(strlen(theString) - strlen(searchFor) + strlen(replace) >= size -1) {
size += 80;
// theString = strcpy(theString,theString);

[Code].....

View 4 Replies View Related

C++ :: Does Realloc To Smaller Memory Result In Same Pointer

Aug 10, 2014

If I do this:
void* testPtr = malloc ( 1000 )

And then this:
testPtr = realloc ( testPtr, 500 )

Will the realloc just reduce the allocated size and keep the same pointer, or can there be a chance of it finding another place for that allocation ( Meaning that it will expensively move the memory to another location )?

I am trying to create efficient programs by making my dynamic allocations the least resource hungry as possible during runtime.

View 2 Replies View Related

C++ :: 2-Dimensional Character Strings And Realloc Function

Jan 19, 2012

The following code fails/crashes in the second loop where it prints the content and frees the memory. But the issue could be with the first loop.

int main(int argc, char *argv[]){
char **a, buf[80];
int x;
a = (char **)realloc(NULL, sizeof(char *));
for(x = 0; x < 5; x++) {

[Code] .....

View 3 Replies View Related

C :: How To Save Char Arrays To Disk

Dec 29, 2013

lets say I have a char array with four elements but only one char is used, does it write four elements or just one?

View 3 Replies View Related

C++ :: Comparing Two Char Arrays Alphabetically

Sep 14, 2013

I want to compare alphabetically two arrays. I try this:

char a[10] = "AABC";
char b[10] = "ZABC";
if(a > b) cout << a;
else cout << b;

But it always outputs a. How can I compare char arrays?

View 3 Replies View Related

C++ :: Template Specialization For Char Arrays

Jun 8, 2012

I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:

Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {

[Code] ....

View 4 Replies View Related

C++ :: Save Top 10 Scores In Simon Game - Filling Array Not Working Correctly

May 17, 2013

I wrote a Simon game, and wanted to save the top 10 scores. I was working right, until I decided I wanted to still be able to read the file if someone enters a name containing spaces. Now, the results aren't right.

void FillScoreList(string Simon_Names[], int Simon_Scores[]) {
ifstream Simon_HiScores("Simon_Data.txt");

if (Simon_HiScores.is_open()) {
for( int x=0;x<10;x++){

[Code] ....

Even without trying to read names with spaces, I'm getting

dad 1
0
340176
0
... either a long number or a zero. No names

View 4 Replies View Related

C :: How To Print The Max Number Row In Relation To The Char Arrays Row

Mar 30, 2013

I have most of the code working properly, but I'm having trouble with a certain area. Currently I have multiple 2D arrays. One is a char array and the other is an int array. In the int array I have to find the max number in each column, which I've done. The problem is, I need to print the max number's row in relation to the char array's row.

For example,

Code: int array[2][3] = {60 50 30 0 100 1}

The max numbers are 60, 100, 30.

char array[2][length+1] = {nameOne nameTwo}

How it needs to print:

nameOne has max score of 60.
nameTwo has max score of 100.
nameOne has max score of 30.

I just can't understand how to compare the two arrays in the right way, so it'll know that nameOne is associated with the numbers in row 0 and nameTwo in row 1, etc.

View 1 Replies View Related

C++ :: Char Arrays - Getting User Input Through Different Methods

May 29, 2014

I've been experimenting with char arrays and getting user input through different methods.

int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;

[Code] ....

As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.

But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.

View 7 Replies View Related

C++ :: Send Two Or More Char Arrays Over TCP Socket (winsock)

Nov 21, 2013

send(sConnect, (userinput, key), 256, 0);

sConnect is already predefined as a socket

Key is a character array of [32]

Userinput is a character array of [256]

How can you send both of them at the same time using the send function without having to create a separate connection?

View 1 Replies View Related

C++ :: Passing Char Arrays Between Functions Of Same Class

Nov 16, 2013

I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.

Here's my code:

Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph

[Code] ....

View 4 Replies View Related

C++ :: Comparing Char Array To Char Always Returns True

Dec 23, 2014

I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.

this is the entire code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code]....

at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.

View 4 Replies View Related

C :: Eliminating Use Of Malloc / Free When Copying Char Arrays

Apr 6, 2014

This program works as expected:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
}

[code]....

But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?

View 1 Replies View Related

C :: Char Array With A Phrase To Char Word

Nov 28, 2013

I need to do a function that copy every word from a text to a char word. How can i do it?

View 5 Replies View Related

C :: Parsing Char Array To Array Of Struct To Process Packets

May 28, 2013

I wrote this simplified version of a program i am writing that parses data in UDP packets. In the process of doing so i pretty much answered all my questions and fix all the problems i was having.

decodeSystemMap function will be in loop, and will proccess packets that have mostly the same data, only a few items will be added or changed or deleted.

whats the best way to check if there are any new, deleted, or removed items in the packet and only modify those?
Is there anything unsafe / dangrous about the way the code is now?

Code:
/* * File: main.c
* Author: david
*
* Created on May 23, 2013, 11:57 AM
*/

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

[Code] ....

View 4 Replies View Related

C++ :: Convert Uint8 Array To Unsigned Char Array?

Mar 2, 2012

I have the following code which attempts to assign a u_int8 array of 256 to an unsigned char[256]:

Code:
unsigned char testData[256]=pSample->data;

I get the compilation error:

error C2440: 'initializing' : cannot convert from 'const uint8_t [256]' to 'unsigned char [256]'

What is the safe way to cast or convert here?

View 3 Replies View Related

C :: How To Convert Char Array Into Int Array

Jan 24, 2013

I am writing code to multiply two int arrays and in my one function i am trying to convert the char array into an int array. I have tested many parts however i can not find the problem.

Code:

struct integer* convert_integer(char* stringInt){
struct integer *converted = malloc(sizeof(struct integer));
int length, i, *ints;
ints = (int *)malloc(10001 * sizeof(int));
length = strlen(stringInt);
printf("stringInt: %s with length of %d
", stringInt, length);
converted->size = length;

[Code]...

View 5 Replies View Related

C++ :: Get Word From Char Array

Jan 28, 2015

Let's say i have some text in char array Code: char text[] = "Hello my friend"; How can i get a seperate words from it? It should be like

Code:
char a[] = "Hello";
char b[] = "my";
char c[] = "friend";

Is it possible to do?

View 2 Replies View Related

C :: Int Array To Char (Binary Value)

Jul 19, 2013

I have an int array with the binary value of a char. How to to turn it into a char? I can transform it to an int but then I'm stuck.

Code:
#include <stdio.h>
int main() {
int a[260] = {0,1,1,0,0,0,0,1};
char buf[260];
int b=0;
for(int i=0; i<8; i++) {
b=10*b+a[i];
}
itoa(b, buf, 10);

View 6 Replies View Related







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