C++ :: Comparing Char Pointers To Integer Pointers
May 21, 2013
I am a little confused while comparing char pointers to integer pointers. Here is the problem:
Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";
When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;
As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};
If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
I have wrote the code below that compares to strings and sees if they are equals doesn't matter the order of the words . In the question we where asked not to use any library functions like the string functions in <string.h> and we have to do that all with pointers . I debugged my code and for some reason the first loop in the function keeps looping ...
#include<stdio.h> int IsEqual(char* str1,char* str2); #define SIZE 20 void main() { char str1[SIZE]="my name is monaya",str2[SIZE]="name monaya is my"; if (IsEqual(str1,str2))
I'm learning OpenGL using the C API and some of the functions' argument types have proven a bit challenging to me.
One example is the function Code: glShaderSource(GLuint shader, GLsizei count, GLchar const** string, GLint const* length); It resides in foo() which receives a vector "data" from elsewhere Code: void foo(std::vector<std::pair<GLenum, GLchar const*>> const& data); To pass the pair's second element to glShaderSource's third argument, I do the following:
1. Can I initialize a char const** via initialization list, the way I do a char const*?
Code:
// this works std::vector<std::pair<GLenum, GLchar const*>> const shader_sources = { {GL_VERTEX_SHADER, "sourcecode"}, {GL_FRAGMENT_SHADER, "sourcecode"} }; // but is this possible?
I have the following code. According to this the values of pointers p[0] and p[1] remains unchanged since the swap is made to local variables in swap function.Now my doubt is how can I swap the pointers p[0] and p[1] inside the function swap??
I'm new with working with random binary files. I have a class with a char* pointer stored inside of it, I also have a constructor that takes in a string (of any size) from the user. I then simply store this string into the char *. Once the string is stored in the char *. I reinterpret the instance, and I store the information into the random binary file. Everything works.
Question: Random files must know the size of the object that is being stored inside of it, so why when I enter strings of different sizes into the file, it appears to still be working. for example this is an example of the code:
class info { private: char *phrase; public: info(string n ="unknown"){ phrase = new char[n.size()+1];
[Code] ....
My point is, lets just say for example the object ETC, was some long string, this would still work for me. My question is, I don't believe each object is the same size because I allocate memory for the char pointer in the constructor.
Should I not do this just to be safe, and just use a char array instead of a pointer? (Even tho I would have set a pre-defined size for the string) or is something happening in the back to prevent this from not working?
I'm trying to write a function that takes a 32bit address and a data to store at this address.
I'm wanting to take the 32 bit memory address eg 0x12345678 and split it into 4 x 2 bytes 12, 34, 56, 78
then each of the 4 entries is at most a 256 entry array.eg FF, FF, FF, FF
So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.
After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.
It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.
void cpu::store(unsigned int mem_add,unsigned int mem_val) { int first = (mem_address&4278190080)>>24; int second = (mem_address&16711680)>>16; int third = (mem_address&65280)>>8; int fourth= (mem_address&255);
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.
double x=1.00,y=2,z=4; if (y/z||++x) x+=y/z; printf("%f ",x); So (y/z||++x)
is true if at least one expression is true, that is either (y/z)!=0 or (++x)!=0 or both. I wonder how the comparison is done? Is (y/z) be truncated to integer or 0 be promoted to double?
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
I am working on a program to find uppercase, lowercase and digits in a 2D char array. When I try to use an if statement to increase the counter, I get an error "no conversion from 'int' to 'char*'". This is the if statement I am using.
I have an array of const char's that are randomly selected in a loop from a list and id like to compare every newly selected choice to be tested against all the others to make sure the same choice isn't given more than once, however the names are lengthy and, for example, using:
for (int x = 0; x<10;x++)//initial loop while ((choice[x]== choice[x-1] || (choice[x] == choice[x-2]) || etc...) //^given that I have 10 variables
Would be messy and a painful sight. What would a more convinient way to check each choice?
Edit:It should be said that each choice would then be randomized again and then checked again, and that each newly selected choice is then immediantly used after this. It'll also be assumed that not all the choices have been made when this part runs(ergo choice[3] may not exist yet) as it is in a loop
I have function that looks like this myfoo(char* Name) Now i want to compare this name to another one . But the another name is a pointer . This my code :
If you did that bob[0] would not equal 11. All well and good right?
Now if you do this?
int sally = 33; test(sallay);
This wouldn't work at all you actually have to use
void test(int& test) { test += 10; }
how the memory addresses etc. are working here? I don't understand why you need to use & the reference operator if it's not an array? Why wouldn't that still work?
Alright, after what seems like forever I'm on the last stage of modifying my Payroll Program. This time I have to sort using pointers and I only have to sort the Net Pay category.
Using one of my earlier programs I've removed content from it until it's in a state where it runs but contains no sorting functions.
So now I've to a working program that lacks any kind of sorting and looks like this.
#include <iostream> #include <fstream>//file input output stream #include <iomanip> #include <string> using namespace std; //function prototypes int readalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[], int n);
[Code] .....
My hints for how to add pointers to the program are...
int *np,tmp; for (i=0,i<n; i++) np=&netpay[i]; //associating the netpay to np; temp=np[i];//storing the pointer to a temp
I've been fooling around trying to sort the Net Pay by pointers using these hints for a bit now and I'm still not getting it.
but I can't seem to extend this to 64-bits. I've tried #if __SIZEOF_POINTER__ == 4 enum constants { UNDEFDATA = 0xDeadBeef }; }; // enum constants #elif __SIZEOF_POINTER__ == 8 enum constants { UNDEFDATA = 0xDeadBeefDeadBeef }; #endif
with: if (ptr == UNDEFINED)
but get a message saying the '==' is undefined (I understand this)
Is there any way to setup so that I can change the size of my constants so that the comparisons will always work correctly? I've tried a 'typedef' but the compiler complains at
'typedef unsigned long long ADDR' // won't accept, and static const SlipCellBase * const TEMPORARY = (SlipCellBase&)0xFFFFFFFFFFFFFFFF; // illegal conversion