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
ADVERTISEMENT
Feb 8, 2014
I have an array of strings and a two dimensional array made up of floats. In the output for the strings (this array stores 10 divers numbers) I am getting only 8 of the numbers, then a core dump. What could cause this?
View 17 Replies
View Related
Jan 9, 2015
I have a firmware application that works well where I display simple text messages on an LCD screen. At the moment, the User Interface is only in English and I have the text strings simply declared as follows:
Code:
static char msg1[] = " Welcome ";
static char msg2[] = " Data Read ";
//etc...
I want to add a variable that can set the language dynamically from an external device, and so I thought I could do the following:
Code:
#define ENG 0
#define FRE 1
#define GER 2
//...
static char msg1[ENG][] = " Welcome ";
static char msg2[ENG][] = " Data Read ";
[Code] .....
writeLCD(msg1[cLang][]); When I try to compile the above I get a compiler error at the "static char msg..." declarations. The error is: "error: array type has incomplete element type"
Is this method valid, but I have a simple syntax problem with the arrray declarations? Or is this method unworkable and I should find a different method?
View 6 Replies
View Related
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
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
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
Aug 20, 2013
I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function
#include <iostream>
#define random(x)(rand()%x) // for random number between numbers of 0 and 1
using namespace std;
void proc1 (int iArray[][2]);
void proc2 (int iArray[][2]);
void selectfunction(int iArray[][2]);
int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };
[Code]...
View 1 Replies
View Related
Nov 24, 2013
I have this code for a computer project... (store management) but the character strings are not copied on text file..
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()
[Code] .....
Now when i run the program, it gives a error :::
ERROR
address 0x0
How can i write these strings to the text file?
View 2 Replies
View Related
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
View Related
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
Nov 8, 2014
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
vector<int> flatten(int a[100][200]) {
vector<int> v;
for (int i = 0; i < 100; ++i) {
[Code]...
I made a function for two dimensional arrays with vector, but I don't know how to make the test code for it.
View 4 Replies
View Related
Sep 4, 2014
If a function takes N-dimensional array as a parameter, what is generic way to do that? I couldn't figure out a nice way to do that.
View 7 Replies
View Related
Jul 31, 2013
How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
and for the one dimensional array as:
int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);
But I do not know how to make a 3-dimensional array or vector.
View 3 Replies
View Related
Apr 4, 2015
This seems simple enough but I'm missing something here to make this code work.What I'm trying to do is print the contents of the two dimensional array in 25 rows and 4 columns. I experimented with something similar to this code when I initialized the array with numbers.
#include <iostream>
#include <iomanip>
#include <cmath>
[Code].....
View 1 Replies
View Related
Apr 4, 2015
I'm getting garbage. I tried including the name of the array in the cin object but I got an error message that says "no match for operator '>>'. When I take the name of the array out, the program compiles but I get garbage. Program is suppose to reads data from a file of 25 students and calculates test scores and class grade average output each student's name score, letter grade and grade average. I would be happy at this point if I could just print out something that wasn't garbage.
Here is the code.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
//const
const int Array_Row = 25;
const int Array_Col = 4;
[Code] ......
View 1 Replies
View Related
Jul 12, 2012
Looking for info about the typedef 2-dimensional array ....
Suppose I have typedef two_Darr[3][3];
How to declare this and how to pass it to other function definition in the main function ....
View 3 Replies
View Related
Jan 17, 2014
I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.
[code]....
I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!
View 2 Replies
View Related
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
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
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
View Related
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
Feb 20, 2012
I have a multiple choice function below that is part of a larger program but I am having trouble having it operate with characters as inputs and arguments. It has to be a character input, but I kinda want it to act like an integer, how do i do that?
Code:
void addition(int *Preward) //function that is called by the users selection {
int random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char a, b, c; // ERROR
[Code] ....
View 2 Replies
View Related
Mar 26, 2014
None of my string is coming out onto my output file..
void OutputHeading (ofstream& fout, string CLASS_EXERCISE,
string PROGRAMMERS_NAME);
void OutputDivider (ofstream& fout, int WIDTH,
char symbol);
void OpenFiles (ofstream& fout, ifstream& fin);
[Code] .....
View 1 Replies
View Related
Oct 6, 2014
I am suppose to make a program that when the user is asked "Enter a Letter for the day:" if the user enters M or m then the screen will output "The day of the week is Monday" and so on until Sunday. I looked over my code and everything looks right but I still get errors saying Missing terminating character " and int function main error.
#include <iostream>
using namespace std;
int main() {
char day;
cout << "Enter a letter for a day of the week: ";
[code]....
View 1 Replies
View Related
Dec 3, 2014
I am writing a driver program that will eventually need to pass two strings to a function I am writing in a separate file. I am reading data from a file that is formatted like this:
ac: and
amo: love
amor: love
[Code].....
Only a few of the words are actually being processed in the way I want. It appears that only words followed by a comma are processed correctly.
View 1 Replies
View Related
Sep 14, 2014
I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?
#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {
[code]....
View 6 Replies
View Related