C/C++ :: Declaring Array Using Sizeof?

Oct 29, 2013

I have the following code:

struct mystruct {
   char fieldA[5];
   char fieldB[7];
};  
void dosomething(struct mystruct* pms) {

[Code] ....

Is there any problem doing that, even for a C89/90 compiler?

View 3 Replies


ADVERTISEMENT

C :: Declaring Array In If-Else Construct

Apr 4, 2013

Code:

if (IS_LEAP_YEAR(year))
const int days_per_month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
const int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Is it ok to declare the array in this manner or is it bad?

And i have to ask the user for a date to enter in my program. So should I use scanf or should I store the date in a string and then use sscanf. I have to check for valid input for everything like day, month, year etc. I did it as below..

Code:

int assignments;
assignments = scanf("%d / %d / %d", &month, &day, &year);
fflush(stdin);
if (assignments != 3)
{
printf("Retry: ");
}
else
error checking.

View 9 Replies View Related

C++ :: Declaring Array To Be Empty

Jun 30, 2014

We have been assigned to create an iTunes library. Everything compiles in my other .h file but my main is not happy with my object declaration. It keeps stating "primary expression before '{'". Here is my main code:

#include<iostream>
#include<string>
#include<fstream>
#include"myTunes.h"
using namespace std;
//function protocols
void read(string);

[Code] ......

View 1 Replies View Related

C++ :: Declaring Array To Be String?

Mar 18, 2013

I'm writing this program that basically interprets the rottentomatoes website. I am however having a problem declaring if it is rotten or fresh according to the rating the user enters.

I'm outputting it here:

void PrintAll(const string titles[], const int ratings[], int count) {
WriteLine('=', 50);
cout << "PRINT ALL" << endl;
WriteLine('-', 50);

[Code] .....

And here is my condition:

string RatingToString(const int ratings[], int count) {
string rank;
for(int i = 0; i < count; i++) {

[Code]....

Here is the output:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
==================================================
MENU
1. Add Movie
2. Print All
3. Exit
--------------------------------------------------
Enter 1-3 : 1
Title : Hitch
Rating : 90
==================================================

[Code]....

My condition works when there is only one movie, but when I add more, it gives it the new movies "ROTTEN" or "FRESH" rank.

View 1 Replies View Related

C++ :: Declaring Array At Runtime?

Oct 21, 2012

I can not set the size of my array while running porgrama. Is there any way to do this in C + +?

---- code ------

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

[Code].....

View 5 Replies View Related

C :: Declaring Array Within Function Argument

Sep 14, 2013

I have a function

Code:

int exec_program(char * arguments[])
{
...
}

I can call it like this without a problem:

Code: char * uselessvariable[] = {"/bin/echo", "Testing", NULL};exec_program(uselessvariable);

However I get an error if I try to compile it like this:

Code: exec_program({"/bin/echo", "Testing", NULL});

How, in c, I can put this array inside of the argument in one line without having to name a new variable name?

View 2 Replies View Related

C++ :: User Declaring The Size Of Array?

Dec 1, 2014

is it possible to use this code ?

#include <iostream>
int main(int argc, const char * argv[]) {
int number = 0;
std::cout << "napis cifro za array
";
std::cin >> number;

[Code]...

It does work on xcode but doesn't in microsoft visual studio 2013. Where is the problem?

View 3 Replies View Related

C++ :: Declaring Array Inside A Class

Apr 18, 2013

class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------

int numLights;
int lights[MAX_LIGHTS];

[Code] .....

I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.

How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.

View 6 Replies View Related

C :: Declaring And Initializing 3000 Number Array?

Oct 23, 2013

Here are the steps to this program:

1. Declare an array that will hold 3000 numbers
2. Initialize this array by assigning a random number to each element in the array
3. Traverse the array, modifying the current contents of each element in the array so that each value now lies between -3000 and 3000 inclusive
4. Traverse the array to compute the average value of all elements

I have never worked with arrays before and am lost!

View 2 Replies View Related

C :: Declaring Array Size Using Variable Causes Program To Crash

May 10, 2013

I am new to C. I've been trying to use C to code some statistical functions originally coded in R. I've encountered an interesting phenomenon. In the function foo1, I declared the array v1v2b using an actual value 1999000. The function runs fine when I call it in R.

Code:
void foo1(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));
outer_pos(x, y, nsamp, &v1v2[0]);
double v1v2b[1999000]; //<-------HERE
for(i=1; i<= 1999000]; i++){
v1v2b[i-1]=1;
} }

However, in foo2, I first create an integer variable called index, and store the value 1999000 in it. I then use it to initialize the same array. When I tried calling this function in R, it either led to a stack overflow error, or completely crashed R.

Code:
void foo2(double *x, double *y, int *nsamp){
int i, j, k, oper=2, l;
double* v1v2=malloc(sizeof(double)*((*nsamp)*(*nsamp-1)/2 + 1));

[Code] .....

View 9 Replies View Related

C :: Declaring Pointer Variable To Array Of Function Pointers?

May 24, 2013

I am having following pointer variable declaration

Code: Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};

Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.

View 2 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

View 9 Replies View Related

C++ :: Flash Card Type Console Application - Declaring Array Of Hexadecimals

Dec 26, 2013

I'm making a flash card type console application using visual studios 2013. The flash cards contain character that I can display using unicode. So far I am looking at about 200 characters across 2 unicode blocks which I don't want to hard code into my arrays. I thought of initializing my arrays using a loop. The only problem is I don't know how to add in hexadecimal. So is there a way to initialize my array without having to input 200 values my self? Also is hexadecimal addition possible without me having to write a function for it?

View 3 Replies View Related

C++ :: Declaring New Instance Of A Class

Nov 6, 2014

I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.

Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){

[Code] ....

View 9 Replies View Related

C :: Arguments When Declaring A Function?

Jun 5, 2013

I am a bit confused about how specific one must be with arguments when declaring a function. I'll show you two functions from the book I'm using to learn C to show you what I mean.

Example 1 (greatest common denominator function):

Code:
void gcd (int u, int v) {
int temp;
printf ( "

[Code] ....

So in that function, there are exactly two arguments, because that's how many arguments the algorithm to find the gcd takes. No problem there, makes sense to me. Then further in the chapter on functions I run into this,

Example 2 (square root function):

Code:
float absoluteValue (float x) {
if ( x < 0 )
x = -x;
return x;

[Code] ....

In this second example, we have a square root function that is preceded by an absolute value function. The absolute value function has the one argument, "float x", however when this function is called within the square root function, the arguments "guess * guess * -x" are passed to it. I'm confused how this absolute value function is working with all of that inside it, when it was originally declared with just "x." The only possibility I can think of is that this expression is treated as a single unit, but I'm not sure.

View 2 Replies View Related

C :: Declaring Variable In A For Loop

Aug 1, 2013

Code:

#include <stdio.h>
int main(void){
int a=0;
for(;a<=10;)
int b;
return 0;
}

I have got a code like this. I don't expect to get an output but just assumed I would see the command screen until I terminated it. What I want to do is just declare a variable b in a endless loop. But what I got from the compiler is this error: error: expected expression before 'int'. I am using Code::Blocks and I think the compiler is GCC.

View 4 Replies View Related

C++ :: Forward Declaring Typedefs

Jun 13, 2014

I'm using some rather large external libraries, and I want to load them in my .cpp file only. so, my header looks like this:

namespace {
// hidden declarations
namespace geometry {
class Point;
class Polygon;
class Box;
//etc
}
}

In the declaration of the main class in that header, I merely use these as pointers or references. The .cpp file looks as follows:

// Boost
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>

[Code] .....

This doesn't work however:

error C2371: '`anonymous-namespace'::geometry::Point' : redefinition; different basic types
1> e:..........NavigationMesh.h(10) : see declaration of '`anonymous-namespace'::geometry::Point'

What can I do to forward declare external classes like this?

View 13 Replies View Related

C++ :: Declaring A Valarray Of References?

Sep 26, 2014

Is it permissible to declare, for example, `std::valarray<int&>`? If so, how do I initialize such if the `valarray` is a class member?

View 3 Replies View Related

C++ :: Can Override Functions Without Re-declaring?

Sep 9, 2014

see these sample:

Code:
#include <iostream>
#include <string>
using namespace std;
class test{
public:
virtual void ola() {

[Code] .....

Like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class. The compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgetting that, how can i override it without re-declare it?

View 10 Replies View Related

C++ :: Declaring Variables In A Function?

Nov 12, 2012

This is a c program that is failing to compile. The error occurs in the calcLabs() function. The error called out is (btw, I'm using VS 2010): Error4error C2143: syntax error : missing ';' before 'type'

I don't understand why the compiler is not letting me declare variables in the calcLabs() function!

Code:
#include<stdio.h>
#include<stdlib.h>
void calcPercent(double *);
double calcLabs();
double calcExams();
double calcFinal();
char calcLetter(float);

[code]...

View 5 Replies View Related

C++ :: Functions - Declaring First And Defining Later In Program

Apr 6, 2013

I am looking at functions still and can't see the point in declaring the function at the top of the program, and then defining later i.e.

Code:
#include <iostream>
int add (int x, int y) {
return x + y;

[Code] .....

I obviously don't have much real world experience with this and am interested to see where declaring and defining later would be useful and/or beneficial.

View 14 Replies View Related

C++ :: What Are The Implications Of Declaring Data Types But Never Using Them

Mar 26, 2013

What are the possible problems if I declare a bunch of data types and never use them? Do they take up a lot of memory? Will they slow run time? If it is an array do I have to delete it at the end of the program? What if the array is defined inside a class and never used? Do I still have to delete it?

i.e.

Code: class declarearrays{
public:
double **darray;
double **darray2;
void function1();//function that initializes darray
void function2();//function that initializes darray2 with different parameters, may not be used.
};

View 1 Replies View Related

C :: Why To Set Length Of Inner Arrays Declaring A Function

Oct 23, 2013

I'm just wondering, why you have to set the length of the inner arrays declaring a function. In which moment does the code needs to be sure about the length of the inner arrays accessing an cell?

I came up with this question realizing the elements of the outer array beeing pointers to the first value of each inner array. Therefore I can access e.g. the first first element of the second inner array like this:

**(arr + 1) ...regardless of the length of any array to my mind.

parallel post: [URL]...

View 6 Replies View Related

C++ :: Character Sequences - Declaring Arrays?

Apr 1, 2014

According to [URL] ....

" char myword[] = { 'H', 'e', 'l', 'l', 'o', '' };
char myword[] = "Hello";

In both cases, the array of characters myword is declared with a size of 6 elements of type char: the 5 characters that compose the word "Hello", plus a final null character (''), which specifies the end of the sequence and that, in the second case, when using double quotes (") it is appended automatically."

However there are examples where word array is declared as :

char myword[] = { 'H', 'e', 'l', 'l', 'o' };

Is syntactically correct?? If yes, what is the size of myword? and does '' append by compiler automatically?

And is char myword[] = "abc" same as char myword[] = {'a','b','c',''} or is it same as char myword[] = {'a','b','c'} ??

View 5 Replies View Related

C++ :: Declaring Parallel Arrays As Functions

Oct 23, 2013

i'm facing some problems with the array, as I have my .h and .cpp files so do i declare them as per norm of how we usually declare a function?

pointtwod.h
class PointTwoD {
private:
int xcord,ycord;
float civIndex;
LocationData locationdata;

[Code] ....

when i compile the error message i get was even when i i put in int xord[]; in my PointTwoD.h file:

PointTwoDImp.cpp:99:6: error: prototype for 'void PointTwoD::storedata(int*,int*,float*) does not match any in class 'PointTwoD'

PointTwoD.h:48:8: error: candidate is: void PointTwoD::storedata(int, int, float)

PointTwoDImp.cpp: 135:22: error: 'xord' was not declared in this scope
PointTwoDImp.cpp: 135:27: expected primary-expression before ']' token
PointTwoDImp.cpp: 135:30: error: 'yord' was not declared in this scope
PointTwoDImp.cpp: 135:35: expected primary-expression before ']' token
PointTwoDImp.cpp: 135:38: error: 'civ' was not declared in this scope
PointTwoDImp.cpp: 135:42: expected primary-expression before ']' token

View 1 Replies View Related

C++ :: Syntax For Declaring Two-way Friendship In 2 Classes?

Feb 24, 2013

What is the general syntax for declaring two-way friendship in 2 classes?

View 1 Replies View Related







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