C :: Assigning Value During Pointer Declaration
Aug 31, 2013
I am trying to understand the behavior of following code. Basically how does printf() prints the value rather than address.
Does initializing value to a pointer during declaration makes a difference when assigned from a variable?
Code:
1 #include <stdio.h>
2
3 int main() {
4 const char *var1 = 'A';
5 int *vint = 10;
[Code] ....
View 8 Replies
ADVERTISEMENT
Feb 3, 2013
I thought we needed to allocate memory before assigning a value to a char* and also that we needed to use functions like strcpy() to copy something into it. Then how come this works and does not crash?
Code:
#include <iostream>
using namespace std;
int main()
{
char * buf;
buf = "Hello";
cout << buf << endl;
buf = "World!!!!!!!!";
cout << buf << endl;
return 0;
}
View 3 Replies
View Related
Jul 2, 2013
In the current code,We are using pointer of union and assigning value.
class sample {
union {
short *two_int;
int *four_int;
double *eight_real;
char *one_ascii;
// void *v;
}; }
Than we assign value in following way.
sample.four_int[0] = (x + xoff); ( x and xoff and y and yoff all are integer)
sample.four_int[1] = (y + yoff);
Than we write data into file. it was working fine into 32 bit machine but it is not working 64bit machine. When I compare data and found that data is divided by 4. For Ex The File generating from 32 bit machine contain 80 than 64 bit . File contain 20.
View 7 Replies
View Related
Sep 13, 2013
In another forum, this example code fragment was stated as being an example of undefined behavior. My understanding is that a literal string exists from program start to program termination, so I don't see the issue, even though the literal string is probably in a different part of memory.
Code: /* ... */
const char *pstr = "example";
/* or even */
char *pstr = "example";
/* as long as no attempt is made to modify the data pointed to by pstr, */
/* unless pstr is later changed to point to a stack or heap based string */
View 11 Replies
View Related
Aug 16, 2013
I have the following code.
StackElement *StackElementArray;
StackElementArray = new StackElement[iMaximumDepth];
I want to assign one element of this array StackElementArray the address of another object. For example,
voidStackMem::push(StackElement &iStackElement) {
CurrentDepth++;
StackElementArray[0] = iStackElement;
}
The StackElement class contains pointers to some dynamic arrays. When I use the assignment, StackElementArray[0] = iStackElement;, it doesn't copy the complete contents and I have to define an 'assignment operator' function to copy all the contents. I am wondering if there is a way I can assign StackElementArray[0] the pointer to the StackElement object. In that case, I will not need to copy the contents of iStackElement into StackElementArray[0] and will just copy the address.
View 3 Replies
View Related
May 9, 2013
I've got a struct called Node that contains, among other things, a pointer to a vector of pgm objects. (pgm is a class i've created)
struct Node {
int label;
vector <pgm> *ptr;
Node* lessNode;
Node* moreNode;
};
in another class, i create a vector and a Node and am having trouble assigning the pointer in the Node to point to my new vector.
vector <pgm> lessData;
Node* left;
left->ptr=&lessData;
This all compiles ok, but the last line in the code above causes a segmentation fault. I should mention Node is declared on its own in Node.h and what pgm is. including pgm.h in node.
View 2 Replies
View Related
Aug 2, 2014
I have the following:
int Allocate(int, int *);
main() {
int *Pointer;
int Elements = 25;
// this works just fine - as expected.
Pointer = (int *) malloc(Elements, sizeof(int));
// This DOES NOT - The value of Pointer never changes.....
[code]....
View 2 Replies
View Related
Nov 27, 2014
im trying to port a code from vc6 to vs2013 and im having this error
Code:
Error11error C2440: 'initializing' : cannot convert from 'char *' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<char>>>' on this line
Code:
vector<char>::iterator BufIt = (char*)lpBuffer;
what i do with this is to stack fragments of data of type char* coming from a socket in buffer to a vector that acts as buffer, I do this since I transfer big chunks of data and the data gets fragmented by the nature of the sockets, I stack the data once its complete I retrieve the final result from the vector.
this code worked flawlessly for long time but now Im trying to port and compiler throws this error, whats the new way to assign a char array pointer to a iterator so i can stack it in the vector.
View 5 Replies
View Related
Feb 19, 2015
I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration
Code:
SensorBase* const sensor(mSensors[i]);
It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??
View 6 Replies
View Related
Feb 12, 2014
I am having a problem assigning bits a value of 0. The data is a 16 bit integer the bits greater than the 12th bit have garbage either a 0 or a 1. I would like to assign all bits greater than 12th bit the value 0 no matter what their values are. Whats the best approach.
View 5 Replies
View Related
Feb 21, 2014
I am reading the book C Programming Language. On Structures, it says:
"The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members."
What does it mean assigning a structure as a unit?
View 12 Replies
View Related
Apr 13, 2013
Here is the code:
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
double foo [5][5];
This is the warning:warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x...It's referring to the boldened part.
View 1 Replies
View Related
Sep 28, 2013
I've seen code examples for assigning 2 dimensional vectors, but I haven't seen code for assigning more than that. I tried to create a 3 dimensional vector, and the only code the IDE didn't complain about was
int x = 2;
int y = 2;
int z = 2;
vector < vector < vector <string> > >stringvec;
stringvec.assign(x, vector <string>(y), vector <string>(z));
Would this be the correct way of producting a vector[2][2][2]?
View 5 Replies
View Related
Jan 5, 2013
I have created a string array of 5 and I will use that to enter some "tasks", then I also have an integer array of 5 where i can assign priorities to the information in the string
string info[5];
int p[5];
cout<<"Enter info 1"<<endl;
cin>>info[0];
cout<<"Enter priority"<<endl;
cin>>p[0];
etc,
I want to be able to link the priorities that i assign to that string and be able to call the information that has the highest priority. I can only using iostream and string header files so far,
View 3 Replies
View Related
Aug 7, 2014
I want to assign a char to an array inside an if statement after the user has input the grade as an integer, but it has to fill an array with characters, such as:
char grades[5];
int grade;
char A, B, C, D, F;
cout << "Enter da grade" << endl;
cin >> grade;
if (grade < 59) {
grade[0] = F;
[code]....
A, B, C, D, and F won't transfer to the array, thus giving me the uninitialized variable error in microsoft visual studio 2010.
View 4 Replies
View Related
Sep 10, 2012
I have the following C++ code
typedef union UUID {
unsigned char byte[16]; /**< Array of 16 bytes. */
unsigned int ll[2]; /**< Array of two 64-bit words. */
} UUID;
[Code] ......
The compiler complains thus
$ g++ union.cpp
union.cpp: In function "int main()":
union.cpp:15:17: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
union.cpp:15:17: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
union.cpp:15:17: error: no match for "operator=" in "entry.EntryHeader::uuid = {0, 0, 0, 2}"
union.cpp:1:20: note: candidate is: UUID& UUID:perator=(const UUID&)
How do I go about assigning values to this union in C++.
View 3 Replies
View Related
Jul 29, 2013
I'm trying to assign a value to the variables in my struct using the statement:
fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)
but when I try to access them, it looks like the variables (acct.name, acct.num, acct.pin) are all empty.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXAMT 999999.99
[Code].....
View 14 Replies
View Related
Oct 11, 2014
I am trying to make a menu program and was wondering if there is a way to declare something more than once without using a different word.
ex.
#include <iostream>
using namespace std;
double grams, ounces, inches, feet, meters; //units to convert
int choice; //menu choice
int main() {
cout << "Welcome to Measurement Converter" << endl;
[Code] .....
I dont really know how to explain it but im trying to use int choice to make choose a program from a simple list.
View 4 Replies
View Related
Oct 20, 2014
I need to dynamically build a control file for a SQL loader but when tokenizing line values read from a file i am unable to work/assign the last value to a variable even though i can print the actual token value while in the loop.
Below is the code snippet i have and get:
Code:
//headElement declared as headElement[42]
//getCol function is used to decode column keys present in line being read
istringstream ss(line);
[Code] .....
I've attached a sample file as well.
View 1 Replies
View Related
Dec 26, 2013
How to scan a CSV file and assigned variable to each of the integer scanned in a csv file
an example of the csv file:
0001,40,,10
How do I scan the CSV file and assign 0001 to variable student id, the 40 to variable module 01,the 1 without input entered to module02 and 10 to module03
View 6 Replies
View Related
Mar 20, 2013
I have this code :
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
[Code] ...
And this error occued with me I don't know why ???
myHeader.h:42:19: error: expected ")" before string constant
This code is compiled from file called myHeader.h ...
View 2 Replies
View Related
Mar 28, 2014
I'm making a game that requires the user to enter a correct username and password to play. I have a list of accepted ones saved in a txt file. How do i read each line of the file and check it with the one entered? I know how to use strcmp(string1, string2). I just need to know how to move from one line to the next. This is what I have for this part of the program so far. Also, I think im using the feof() wrong.
Code:
id = fopen("F://CS223/namepass.txt", "r");
score = fopen("F://CS223/scores.txt", "a");
srand(time(NULL));
if(id == NULL)
{
printf("ERROR");
[Code] .....
View 6 Replies
View Related
Apr 8, 2014
What I am doing is using fgetc to take a char input from the user, and then directly after that calling a function which will clear the buffer and remove the character. But for some odd reason all my inputs are being assigned before taking the char input by the user. Here is my code for the relative problem:
Code:
char playAgain; Code: playAgain = fgetc(stdin);
readRestOfLine(); Code: void readRestOfLine(void) {
int ch;
/* remove all characters from the buffer */
while(ch = getc(stdin), ch!='
' && ch!=EOF)
;
/* clear the error status of the input pointer */
clearerr(stdin);
}
And en example through terminal of the issue, with print statements debugging the value of play Again:
So in both instances here I have entered 'y', one returning it as ASCII 10 which is ' ' and ASCII 121 which is 'y'.
View 6 Replies
View Related
Feb 3, 2013
I have a problem with rotating an array and assigning it to different variables. This is wat i have done.
Code:
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#include<math.h>
int n[16],a[16],n1[16],n2[16],n3[16],n4[16],n5[16],n6[16],n7[16],n8[16],n9[16],n10[16],n11[16],n12[16],n13[16],n14[16],n15[16],n16[16], c=0;
int x[16],i,j,k;
int rotate();
[code]....
View 4 Replies
View Related
Feb 18, 2014
I'm trying to write a very simple program that takes values in through variables, and stores those values in a two dimensional array. The values are already passed into the void function, I need to have those values write to their corresponding locations in the array.
void planeSeats(int seats[13][6], int ticket, int ticketRow, int ticketColumn) {
if (ticket = 1) {
if (ticketRow >= 1 && ticketRow <= 2) {
[Code] ....
For example, lets say that ticketrow is 2 and ticket Column is 4 .
What I need is for ticketRow and ticketColumn to assign that data to seat[ticketRow][ticketColumn], turning it into seat[2][4]. How to do that.
View 2 Replies
View Related
Nov 19, 2013
My homework wants me to take an input that is "x1888.88" and assign the "x" to a char, the "1" to a char, and the "888.88" to a float. How i can do this? and the "888.88" part could be any length could be a single digit or multiple with a decimal.
View 4 Replies
View Related