C++ :: Adding Char Array With Macro?
Dec 30, 2013
The book uses this example:
#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */
char *alloc(int n)
/* return pointer to n characters */
[Code] ....
The logic here I don't understand:
if (allocbuf + ALLOCSIZE - allocp >= n)
allocbuf is a char array allocated with 10000 positions. We add that to ALLOCSIZE macro which is 10000. Does that return a value of 20000? If so, it doesn't make sense to do this because all we had to do was this:
if (allocbuf - allocp >= n)
That takes length of allocbuf which is 10000 and subtracts allocp from it. So if allocp is 1000 we are left with 9000 available slots. And we can use that to check if we have enough to allocate n elements.
View 5 Replies
ADVERTISEMENT
Nov 22, 2013
I'm trying building a new macro for change the array size:
#define redim(varname,x) ((sizeof(varname)*) realloc (varname, x * sizeof(varname)));
int b;
redim(b,3);
error message:
"error: expected primary-expression before ')' token"
what isn't right with these macro?
View 10 Replies
View Related
Mar 12, 2014
How I can go back to an alphabet letter I want that letter to land on? Adding capital letters. Lower case letters not needed.
For example;
- User enters "XYZ"
- Program comes up with the next 10 letters which would be something like 'HIJ' (Since X+10 = H, Y+10 = I, Z+10=J)
My problem is with the last few letters. The program goes to the next 10 characters ahead of those characters rather then the alphabet.
Program example: (What I want shown)
"What are your three favorite letters?" XYZ
"The next ten letters are: HIJ"
Error example: {It goes to the next 10 characters instead of the next 10 alphabet letters.)
"What are your three favorite letters?" XYZ
"The next ten letters are: ^[a"
Code example: (I've tried other ways as well.)
char a, b, c;
cout << "What are your three favorite letters?";
cin >> a >> b >> c;
cout << "The next ten letters are: " << a+10 << b+10 << c+10 << endl;
View 4 Replies
View Related
Feb 26, 2012
Is it possible to define a macro with in a macro? Any trick will do. I am trying to do quick conversion of cuda program to open mp by defining some macros at the top:
Code:
#define __syncthreads() #pragma omp barrier
View 2 Replies
View Related
Aug 15, 2014
I am making a program that formats a string. I want to create a new 2 dimensional string that will have many other chars and strings in it beside the original string. Then I split the string up on the newlines and return it. Adding different parts to the 2d string e.g. I need to add five _ as chars not string then I need to add different things. First I use sprintf () to add as much as possible. And then I do what to add the rest?
View 13 Replies
View Related
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
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
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
May 21, 2013
How would one add each value from an array? I'm working from a string but I was wondering if there was a way to loop through the string and add each value. This is what I have so far:
#include <iostream>
#include <cmath>
#include <string>
int main() {
std::string numbers;
int sum;
[Code] ....
View 3 Replies
View Related
Sep 28, 2013
I am trying to convert a string Input by user into an unsigned byte array.The data would be in hex form like :- "AE 1F 2C". I am thinking of using strtok to split the string into be " ". and then cast to (unsigned char *) . But for that I''ll have to prefix every element with 0X Is there any convenient way and elegant way to do this?
View 3 Replies
View Related
Sep 28, 2013
I'm having trouble getting my array to add its values together. I have a similar program running fine with the exact same equation. I am not so naive as to believe that this means it should run with every program but I'm at a loss as to what to do.
#include <iostream>
#include <iomanip>
using namespace std;
[Code].....
View 2 Replies
View Related
Jun 28, 2013
When i run the program i want to add a big integer number into an int array. How can i do it.i don't want to use for loop.
View 4 Replies
View Related
Feb 1, 2013
I'm having some issues with adding a record to my array.
#include <iostream>
#include <fstream>
#include <string>
[Code].....
View 2 Replies
View Related
Aug 6, 2014
My program is suppose to be as a virtual phone book that allows you to add,search, display names and numbers.
At the beginning you are able to add up to 10 entries and then from there the program goes to a menu where you can add more entries, search etc.
My problem is that I am unable to add an entry into the existing list of names/phone numbers.
Example: At the beginning I add Joe,Albert,Barry. It sorts them into Albert, Barry, Joe (good so far!)
However, if I choose to add another entry (Carl) it becomes Barry,Carl,Joe.
The functions I am using to add entries are: GetEntries (for initial entries) and Addentries for more entries during the main program.
*******************************COPY OF CODE**********************************
#include <iostream>
#include <string>
using namespace std;
[Code].....
View 5 Replies
View Related
Jan 10, 2015
I have an inventory array in a class called inventory. This class is in a different program. How do I access and add to this array for my main program?
View 1 Replies
View Related
Jan 9, 2015
This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code]....
Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15
View 8 Replies
View Related
Jan 29, 2014
lets say we have a txt that contains this
| | | | |*| |
| | | |*| | |
and that symbolizes a 2x6 char array and we want to take only the symbols inside the || and place them in a 2x6 char array. how do we do that?
View 1 Replies
View Related
Mar 6, 2015
I have a structure product_array *pa that contains a pointer *arr to an array of structs and count that adds 1 when a new product is added (set to NULL initially). I have to write a function which adds a new product entry to that array. One product entry has *title, *code, stock and price parameters. The array is dynamically allocated and I’m supposed to:
1. Reallocate space for array.
2. Update product_array.
3. Initialize it.
Also, code should be truncated to 7 characters.Products can be added multiple times, so the initial size is unknown.
Code:
void add_product(struct product_array *pa, const char *title, const char *code, int stock, double price)
{
for (int i = 0 ;; i++){
pa->arr = realloc(pa->arr, sizeof(struct product_array));
[code]....
View 3 Replies
View Related
Jan 9, 2015
I would just like to share my code and wanted to do something about it. This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
#include <iostream>
#include <conio.h>
using namespace std;
[Code]....
Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15
View 5 Replies
View Related
Mar 9, 2014
The goal of this program is to take 4 neighboring elements in an array and add them together. The program asks user for the number of rows and columns to start out with and the program will then continue to print the board until 1 element remains.
I'm having problems getting my program to compile (line 19)
#include <iostream>
#include <cstring>
#include <cstdlib>
[Code].....
View 3 Replies
View Related
Jan 4, 2015
I am trying to create a small set of filepath functions that I intend to compile across linux and windows (I prefer not to use a big library). I want to have a global constant PATH_SEPARATOR that depends on the OS environment. This is what I set at the top of header file.
Code:
#include <stdio.h>
const char PATH_SEPARATOR =
#ifdef _WIN32
'';
#else
'/';
#endif I was hoping to test this while compiling this in a linux environment using gcc, thusly:
Code:
int main (int argc, char const* argv[])
}
[code]....
where apparently, I seem not to be able to "set" a part of the code to have "_WIN32" defined. I don't know if I explained this clearly.
View 5 Replies
View Related
Mar 22, 2013
I have this macro, that expands to some methods of my class:
Code:
#define POPULAR_COMBO_FILTRO_COM_BASE(classeTabela)
void VisualizadorLogsFrame::PopularComboFiltroCom ## classeTabela (bool limparTexto) {
long from, to;
m_cbxDetalhe->GetSelection(&from, &to);
[Code]...
but I get a compile error on this line:
Code:
for (list< CLASSE_TABELA *>::iterator linha = lista->begin(); linha != lista->end(); linha++) {
what am I doing wrong ? the error is:
VisualizadorLogsMain.h:174: error: expected ';' before 'linha'
VisualizadorLogsMain.h:174: error: 'linha' was not declared in this scope
View 4 Replies
View Related
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
Sep 12, 2013
I'm doing right now is creating a function that callocs (I prefer this to malloc) and returns a string, and it will work similar to printf, I'm calling the function alloCpy(),I have several values that I need in a malloced string, so I call Code: myAllocedString = alloCpy("Value 1 is %s, value 2 is %s, and value 3 is %d", str1, str2, num); To do this I'm using the Variadic Macro, the reason I'm not just using a Variadic Function such as this: Code: char* alloCpy(char *format, ...) {} is because I need to append NULL to the end for the sake of looping through arguments, and I'm understanding it thusfar, but I have a few issues, first of all, I tried defining the Macro in a header file, but when I try to call it I get the error "Undefined reference to alloCpy". Also, to loop through arguments to get string lengths I'm using va_arg(args, char*) which requires all the arguments to be of type char*. Here is my code:
myheader.h:
Code:
#define alloCpy(format, ...) _alloCpy(format, ##__VA_ARGS__, NULL);
char* _alloCpy(char *format, ...); mycfile.c: Code: char* _alloCpy(char *format, ...) {
va_list args;
va_start(args, format);
int args_len = 0;
}
[code]....
So, how can I do this to, first of all, make my macro function accessible from other files importing myheader.h, and second, how can I make it accept any type of argument like printf, so that my example above would work?
View 3 Replies
View Related
Jan 16, 2015
Say I have something lke
Code:
#define FOO BAR
#if FOO == BAR
doX();
#else
doY();
#endif
This causes doX(); to be executed. But the intent is to have doY(); be run. I'm guessing this is because BAR is undefined and therefore blank, so blank equals blank. Is there some way to compare the symbol FOO was set to instead of its value, BAR?
View 4 Replies
View Related
Apr 4, 2013
I heard that const shall be preferred over #define . So I start to change my program accordingly.
But then below error message occurs during compilation:
#include "common.h"
#include "definition.h"
#include "particle.h"
int main() {
Particle *p = new Particle();
[Code] .....
I guess the error occurs because, when the line 9 of particle.h (File 4) is compiled, value of const int dimension is not seen by the compiler.
View 6 Replies
View Related