w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
confusion about char pointer in c++
ptr holds the address to where the literal "hello" is stored at. In this case, it points to a string literal. It's an immutable array of characters located in static (most commonly read-only) memory. You can make ptr point to something else by re-assigning it, but before you do, modifying the contents is illegal. (its type is actually const char*, the conversion to char* is deprecated (and even illegal in C++11) for C compatibility. Because of this guarantee, the compiler is free to optimize for space, so char * ptr = "hello"; char * ptr1 = "hello"; might yield two equal pointers. (i.e. ptr == ptr1)

Categories : C++

I have big confusion to understand the difference between `char *var[3]`, `char var[3][15]`
The first is a two-dimensional array. The second is a one-dimensional array of pointers. If those pointers point to arrays then it will also be a two-dimensional array, but that isn't guaranteed simply by looking at the types. For example, you could have an array of 5 NULL pointers: char *var[5] = {0, 0, 0, 0, 0}; In this case it certainly is not a two-dimensional array, and trying to access var[i][j] will result in undefined behaviour (most likely a memory access error, or "segfault").

Categories : C++

Confusion on when should we use "single-pointer" and when "pointer-to-pointer" regarding datastruture
In this case, the pointer to pointer is used so that it is possible to change the pointer to the first node. You keep a pointer which points to the beginning of the list. If you add a new item, that pointer might need to change since the new item may be added at the beginning of the list. void insertAtBeginning(struct node **first_ptr,int value) { struct node *first = malloc(sizeof(struct node)); first->value = value; first->next = *first_ptr; *first_ptr = first; } void test() { struct node *first = 0; insertAtBeginning(&first,5); freeAll(first); } If you are doing something like searching through the list, there is no need to change the pointer to the first node, so there is no need to pass a pointer to pointer. But note that there are other us

Categories : C

Why doesn't C function with char** param set value of a pointer to char[] arg
The problem is that a pointer to an array is not compatible with a pointer to a pointer. They are different things. And it's a good thing too, because this is meant to catch errors like the one in your code. What you are doing is basically this: unsigned char szrecordid[28]; szrecordid = malloc(i * sizeof **value); You should be able to see the problem; you're trying to assign a pointer to an array.

Categories : C

How to initialize a char array using a char pointer in C
"How come this does not work?" Because that's not how the C language was defined. You can create a copy using strdup() [Note that strdup() is not ANSI C] Refs: C string handling strdup() - what does it do in C?

Categories : C

Want to pass a single char pointer from a double pointer
For simplicity's sake, you can assume a double pointer to be equivalent to a 2d array (it is not!). However, this means that you can use array-convention to access contents of a double pointer. #include <stdio.h> #include <string.h> char *query[5] = {"the","new","store","in","SF"}; char *stopwords[2] = {"the","in"}; char main_array[256]; void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length); int main() { remove_stopwords(query,5,stopwords,2); puts(main_array); return 0; } void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length) { int i,j,found; for(i=0;i<query_length;i++) { found=0; for(j=0;j<stopwords_length;j++) { if(strcmp(query[i]

Categories : C

Dynamically allocating char pointer pointer
Here's what your code is doing: Calling addMessage with "This" Calling this function with messageCount = 0: messages = realloc(messages, messageCount * sizeof(char *)); And then continually writing into unallocated memory. So, do a ++messageCount; at the top of addMessage and you can remove it from the bottom of the function.

Categories : C

C++ const char* overloading confusion
Because arrays and pointers are different types. Try: blah(&a[0]); EDIT: ... which is to say that the template is a better match for the array type, even though the other one would be called if the template didn't exist.

Categories : C++

Pointer to char into char array
You no need to change your return type. You can directly dereference your memory area allocated by malloc() by using index. You are returning base address of the array. So by adding index to that address you can directly access your allocated memory. You can access like this, result[0],result[1],....

Categories : C

confusion about free(pointer) in c
void push(){ int ele; struct stack *temp,*p; printf("entere the element "); scanf("%d",&ele); temp=(struct stack *)malloc(sizeof(struct stack )); temp->data=ele; if(start==NULL){ start=temp; start->next=NULL; } else{ p=start; while(p->next !=NULL){ p=p->next; } p->next=temp; temp->next=NULL; } free(temp); // don't free temp here ! } You have to free a pointer only if you don't need it any more. You could think it is the case, since you don't use temp, but it

Categories : C

Pointer syntax confusion (*ptr vs ptr)
This made me think *ip was used for integer values (since *ip was declared an int) while plain ip was used for addresses, and ip (&x) would "point to" *ip (x). In other words, the address of x would point to x. That's right. Exactly. (Well, almost. It's not that *ip was declared as int, but ip was declared as int *. That's what confused you, see the next part.) Based on the function assignment, it's clear *px = &a and *py = &b No, not at all. px and py are pointers themselves. px = &a and py = &b, and initially *px = a and *py = b (of course after the swap, this changes). What presumably confused you in the second case is that the * character has two meanings in the context of pointers. The one is, when you declare a variable of pointer type, the * charac

Categories : C

Java pointer passing map confusion
Is it the same pointer or does a copy get made? First of all, there is no such thing as pointers in Java. It's called reference. List<String> links = nodeToLinkMap.get(node); In the above code, you are simply creating a new reference to the list stored in the map. It does not create a copy of the list itself. So, whatever you add or remove from this list gets reflected in the list stored in map. You don't need to put the list back into the map. So, you can remove this line from your 1st code: nodeToLinkMap.put(node, links); // Not needed. 2nd code is fine. But it might not look pleasant, if you are going to do too much modification in your list.

Categories : Java

Assigning array to pointer confusion
Array Name is a constant pointer to the first element of array MORE:- http://forum.allaboutcircuits.com/showthread.php?t=56256

Categories : C

Pointer references mixing '*' and ' ->' syntax confusion
You are right, but not entirely. The u_int8_t type serves as an addressing unit. It is basically a "byte". So the ((u_int8_t *)X) + O) part simply selects the location at byte-offset O from the beginning of the memory block pointed by X. That's where the role of u_int8_t type ends. Then the *(u_int16_t *) part interprets that memory location as an unsigned 16-bit word. So, you got everything right except that the macro "returns" a 16-bit value (not 8-bit value) stored at address X + O. In your specific example the macro is used to read an unsigned 16-bit value stored at the very beginning (offset 0) of memory block packet->payload. Note that a macro written in this fashion evaluates to an lvalue, meaning that it can also be used for writing 16 bit data at specific byte-offset

Categories : C

Is a variable defined with "char *" a pointer or a variable of type char in c?
By writing char *myvariable, int foo, double blah, or void (*funcptr)(void), you are declaring a variable with some type. The type of the variable specifies what is stored in that variable: a pointer, number, or structure. So, char *myvariable is a variable (some reserved amount of memory) that contains a pointer value.

Categories : C

C++ cut char pointer
Decryptor dec; char * decrypted = dec.decrypt(_packet + 8, _packet); // _packet[0] - 8 is going to give you the value of the character at _packet[0] minus 8, which is not likely to be what you want.

Categories : C++

pointer of char and int
Strings aren't really "first-class citizens" in C. In reality, they're just implemented as null-terminated arrays of characters, with some syntactic sugar thrown in to make programmers' lives easier. That means when passing strings around, you'll mostly be doing it via char * variables - that is, pointers to null-terminated arrays of char. This practice holds for calling printf, too. The %s format is matched with a char * parameter to print the string - just as you've seen. You could use *a, but you'd want to match that with a %c or an integer format to print just the single character pointed to. Your second example is wrong for a couple of reasons. First, it's not legal to make the assignment b = 1 without an explicit cast in C - you'd need b = (int *)1. Second, you're trying to p

Categories : C

C++ char* cannot be used as pointer
Yes, but cptr[1] is not - it's a char. That's what you're applying * to. *cptr[1] == * (cptr[1]) | this is a char You could just write char cptr[] = "A"; or use a std::string.

Categories : C++

char pointer to pointer
When you pass a char * to op<<, it assumes it's a C-style string and acts accordingly. The address of the second character in a C-style string is no different to the address of the first, in terms of its backing type - it's still a char *. If you really want the pointer itself, cast it to a void * so that it doesn't know what the underlying type is. For example: #include <iostream> int main (void) { const char *name = "=paxdiablo"; std::cout << name << ' ' << (void*)name << ' '; std::cout << &name[1] << " " << (void*)&(name[1]) << ' '; return 0; } outputs: =paxdiablo 0x8048880 paxdiablo 0x8048881

Categories : C++

C : Using char * const pointer
You are getting a Windows error because you are invalidly accessing memory. On other systems you might get a SEGFAULT or SEGV or a Bus error. *p = 'm'; Is trying to change the first letter of the constant string "hello" from 'h' to 'm';

Categories : C

Accessing a char* through a pointer
const char* f_url = "blah blah"; this declaration means that you have a pointer to a constant string. So you can't do strcpy(f_url, "hello world"); // copy to the preallocated place but you can f_url = "hello world"; // do pointer assignment If you have next situation : class ClassA { const char* f_url = "blah blah"; public: void method() { f_url = "hello world"; } }; class ClassB { char** m_url; public: void print() { cout << (* m_url); // m_url points to a string, allocated on the stack of ClassA::method() } }; keep in mind that string "hello world" is allocated on the stack of the ClassA::method(). And that pointer is no longer valid when the ClassA::method() quits. to solve the issue I propose next: class ClassA { static const i

Categories : C++

C++ pointer to char arithmetic
The array isn't storing chars, it's storing char *s. Hence saying ch + 2 will be equivalent to ch + 2*sizeof(char *). You then dereference that, which is pointing to "I'm a string literal". Your initial example shows the confusion: int* num[5]; cout << *num << ", " << *(num + 2) << endl; This is an array of pointers-to-int. Hence, *(num + 2) will be *(num + 2*sizeof(int *)), not 2*sizeof(int). Let's demonstrate this with a small program: #include <iostream> int main() { int *num[3]; int x, y, z; x = 1; y = 2; z = 3; num[0] = &x; num[1] = &y; num[2] = &z; std::cout << *(num + 2) << " "; } This will print out a memory address (like 0x22ff28) because it is holding pointers, not values. In C

Categories : C++

pointer, malloc and char in C
Just replace all the char* with std::string. Do that until you have a very specific reason to not use existing utilities, which is something you don't have as a beginner. None of the code above requires malloc() or raw pointers. Some more notes: const char strng[] as parameter is the same as const char* strng. The array syntax doesn't make it an array, it remains a pointer. I don't use this syntax in order to avoid this confusion. Use static_cast or one of the other C++ casts not the C-style like (char*)malloc(..). The reason is that they are safer. Check the return value of malloc(), it can return null. Also, you must call free() eventually, otherwise your application leaks memory. Finally, the pointer does point to the 'H', which is just the first element of the string. Output *p in

Categories : C

JNA pointer to unsigned char*
Passing a primitive array or a Pointer to memory are equivalent operations, i.e. you can map like this: public interface MyLibrary extends Library { void someFunction(byte[] input); void someFunction(Pointer input); }

Categories : Java

char pointer and printf
The issue is the string "jjhljlhjlhjl" is only a char *, whereas you assign it to a char ***. When you print x, it gets interpreted as a char * by printf, which winds up being the correct string. As for printing &q as a string address, you're effectively printing the contents of q. When you dereference x, you get a char **, which in your case, is 4 bytes. But because x actually points to text data, *x will grab the first four bytes (i.e. characters) of your string. The fact that it's printing only four characters is pure chance.

Categories : C

char pointer value in array
table[i][5+(i/2)] = ptr; is wrong because you are trying to assign address instead of value. table[i][5+(i/2)] = *ptr; is correct. It will give the value at the ptr. Segmentation fault is because of i. it is referring an address which is out of array boundary.

Categories : C

Sort char pointer array in C
The issue is that the values passed to the sort function (a.k.a StringCompare) are pointers into the a array. In other words, they are of type const char **. You need to instead declare char_a and char_b as const char **, and dereference them in the call to strcmp: int StringCompare( const void* a, const void* b) { char const **char_a = a; char const **char_b = b; return strcmp(*char_a, *char_b); } Also note the casts are unnecessary.

Categories : C

What is the JNI equivalent to an unsigned char pointer?
Write your Java Class with native method first, using whatever you expect your data to be when Java receives it. Then call javah on that Class file. This will generate header and stub files for use in C++ which you then extend to design your C++ class. You want to be able to contain all data. Therefore, the largest smallest variable types should be used. As others have noted (I've upvoted them), unsigned int requires a long and unsigned char requires an int, so it breaks down like this: C | JAVA IN C | JAVA unsigned char keywords[][MAX_SIZE] | jobjectArray of jintArrays | int[][] unsigned int *s | jlong | long int slen | jint | int unsign

Categories : Java

C inserting string into char pointer
If you are using constants, then simply reassign the pointer, not the contents: const char* protoName = ""; if(h->extended_hdr.parsed_pkt.l3_proto==0) protoName = "HOPOPT"; else if(h->extended_hdr.parsed_pkt.l3_proto==1) protoName = "ICMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==2) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==3) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==4) protoName = "IGMP";

Categories : C

C free garbages char pointer
I suspect that the string you are copying are not null terminated and when printed might include garbage chars. from man strncpy: The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Categories : C

char pointer as function parameter
Because you print the size of the pointer, and not what it points to. Arrays decays to pointers, and as soon as you pass it to a function you have a pointer instead, and loose all size information of the original array.

Categories : C

behaviour of pointer ; char ** argv
You're using it wrong. *argv+1 will be interpreted as (argv[0])+1 and since argv[0] is "./geip" you get "/geip". *argv+9 will be interpreted as (argv[0])+9 but since argv[0] only has length 6 the outcome is undefined. In your case, argv is probably stored as : . / g e i p 1 2 5 5 0 1 2 3 4 5 6 7 8 9 10 11 Which explains why +9 gets you "55" But you should really forget that because it's never going to be useful! This is undefined behavior and should not ever be used.

Categories : C

Assigning address of an int to a char pointer
You didn't take endianness into account. On a little-endian system, the 'a' (or if the encoding isn't ASCII compatible, whatever 65 is) will be in the first byte, and the other bytes are 0. Passing a 0 byte to printf("%c ",_); prints out nothing but the newline.

Categories : C

C Access 2D char array via Pointer
char **a_ptr = &ary; - this makes no sense. char** a_ptr is a pointer to a pointer. What you need is a pointer to an array. The following will do: char (*a_ptr)[8] = ary; // also don't take the address of the array If you're trying to print the pointer address, printf has a format for this %p. Replace your 0x%xs with %p. I've edited your code as follows and my compiler no longer issues warnings: #include <stdio.h> #include <string.h> int main() { char ary[5][8]; char temp[8]; int i; char (*a_ptr)[8] = ary; for(i=0; i<5; i++) { sprintf(temp, "0x10%d" , i); strcpy(ary[i] , temp); printf("~~~%s@%p == %p" , ary[i] , &ary[i] , (a_ptr + i)); } for(i=0; i<5; i++) { printf("~~~%s@%p" , *(a_ptr

Categories : C

Char array in c & pointer addition
Possible operator precedence bug: Change to: if ((third[3] == second[2] + 8) && (second[2] == first[1] + 8)) The wiki article on 'Order of operations' tells more about the evaluation precedence. If you can't explain to someone in the blink of an eye, in which order some expression will be evaluated - you should use enough parentheses that it won't matter (because no order precedence to care of) or that it's instantly obvious how the clauses are separated. EDIT: A closer look makes me realize something fishy is going on. You're not incrementing any pointers. If you were, you would do something like if ((third[3] == second[2 + 8]) && (second[2] == first[1 + 8]))

Categories : C

how to get char pointer from ostringstream without a copy in c++
Although you can't get a pointer to the character buffer in the ostringstream, you can get access to its characters without copying them if you switch to using stringstream. A stringstream allows input and output (reading from and writing to the stream), whereas ostringstream allows only output (writing to the stream). Example: std::stringstream ss; ss << "This is a test."; // Read stringstream from index 0. Use different values to look at any character index. ss.seekg(0); char ch; while (ss.get(ch)) { // loop getting single characters std::cout << ch; } ss.clear(); // Clear eof bit in case you want to read more from ss This site has a pretty good overview of stringstreams and what you can do with them.

Categories : C++

Is char pointer address initialization necessary in C?
Initialisation is not needed, regardless of what type the pointer points to. The only requirement is that you must not attempt to use an uninitialised pointer (that has never been assigned to) for anything. However, for aesthetic and maintenance reasons, one should always initialise where possible (even if that's just to NULL).

Categories : C

My char pointer points to invalid value after being cast from int*
It's because the size of char is one, and the size of int is four. This means that adding 4 to ptr makes the result point to the second entry in the int array. If you compiled this on a big endian system you would have printed 33554432 instead.

Categories : C

Size of char pointer array from function
It's because char *messages[] is just syntactic sugar for char **messages, so you're just getting the size of a pointer, not the size of your whole array. You can't, without passing the size along as another parameter. You should check out the comp.lang.c FAQ section on Arrays & Pointers.

Categories : C

Using strcmp to compare a char pointer and a literal
You need to pass a char* to strcmp, like args. Here is the prototype for strcmp from here: int strcmp ( const char * str1, const char * str2 ); So since args is a char* and "r" will give you a char* you should try: /* since you're learning, print out what this thing does */ #include <stdio.h> printf("Output: %d ",strcmp(args,"r"));

Categories : C



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.