I'm specifically focused on when to use malloc on char pointers
char *ptr;
ptr = "something";
...code...
...code...
ptr = "something else";
Would a malloc be in order for something as trivial as this? If yes, why? If not, then when is it necessary for char pointers?
...
Hello, i have a simply warning in my iphone dev code.
NSUInteger *startIndex = 20;
This code work, but i have a warning :
warning: passing argument 1 of 'setStartIndex:' makes pointer from integer without a cast
Thanks for your help.
...
Hi all,
What I am trying to do is essentially create two vectors of objects, where some objects are entered into both lists and some are just entered into one. The first problem I found was that when I used push_back() to add an object into both lists the object was copied so that when I changed it from one list the object did not chang...
I have a struct which contains a member called char *text. After I've created an object from the struct, then how do I set text to a string?
...
I'm working on a very basic game and I have a std::list collection of objects that pertain to my game. I declared it as:
std::list<Target> targets;
When I iterate over it, using
for (std::list<Target>::iterator iter = targets.begin(); iter != targets.end(); iter++) {
Target t = *iter;
t.move();
}
My objects are...
Hi. I'm pretty sure I'm doing nothing wrong, but thought I'd ask anyway.
We have:
struct some_struct **array_of_ptrs = calloc (num, sizeof (struct some_struct*));
Now assume I just point each of these pointers in the 'array' to a struct some_struct. Now surely to free up the memory, I just do:
free (array_of_ptrs);
Surely this is...
I have a function defined as the following (in C):
gchar **Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, gchar code)
{
switch (code)
{
case 't': /* Title */
return &FileTag->title;
case 'a': /* Artist */
return &FileTag->artist;
case 'b': /* Album */
...
If I'm using Objective-C, here's how I declare an initialize an int:
int a = 1;
vs an object:
myObj *a = [[myObj alloc] init];
So this is a pointer to an object as denoted by the '*'. My question is, why aren't objects of type id declared this way? I.e., why is it this:
id genericObj;
and not:
id *genericObj;
?
...
Hello,
I am a C beginner, and I am writing a very simple linked-list. I am wondering if there would be a memory leak in the following code:
void removeListEntry(struct tableEntry *symp, struct tableEntry *previous) {
if (symp->next = 0){
symbolList.tail = previous;
previous->next =0;
} else {
previous->next = symp->next...
I have written a simple program which will report key press and release events for a particular window. In my case, it is mostly the terminal since I invoke the program from the terminal. I am able to get the key press and release events taking place in the terminal window (I have used XSelectInput() with KeyPressMask and KeyReleaseMask ...
Basic question.
char new_str[]="";
char * newstr;
If I have to concatenate some data into it or use string functions like strcat/substr/strcpy, what's the difference between the two?
I understand I have to allocate memory to the char * approach (Line #2). I'm not really sure how though.
And const char * and string literals are the ...
This question goes out to the C gurus out there:
In C, it is possible to declare a pointer as follows:
char (* p)[10];
.. which basically states that this pointer points to an array of 10 chars. The neat thing about declaring a pointer like this is that you will get a compile time error if you try to assign a pointer of an array of d...
typedef struct unit_class_struct {
char *name;
char *last_name;
} person;
int setName(person *array) {
array[0].name = strdup("Bob");
array[1].name = strdup("Dick");
return 1;
}
int setLastName(person *array) {
array->last_name = strdup("Sanchez");
array++;
array->last_name = strdup("Clark");
re...
I'm using the ffcall (specifically the avcall package of ffcall) library to dynamically push parameters to variadic functions. i.e. we have
int blah (char *a, int b, double c, ...);
and we want to call this function with values taken from the user. To do this, we create an avcall version of the function:
int av_blah (char *a, int b...
I have a pointer to my char array like this.
unsigned char *recvBuf;
recvBuf = (unsigned char *)malloc(sizeof(char)*RECVBUF);
I then pass that to a function defined as
void setHeader(MyHeader *myHeader, unsigned char *buffer)
Which copies the first 12 bytes into a struct
Then I try and pass it to another function later on ...
typedef struct unit_class_struct {
char *name;
} person;
person * setName() {
person * array;
array = malloc (2 * sizeof(person));
array->name = strdup("Robert");
array++;
array->name = strdup("Jose");
return array;
}
int main()
{
person *array;
arra...
I am trying to pass a structure array pointer and a pointer to a structure array pointer into a function and then have it modified rather than using a return.
This example code is indeed pointless, its just a learning example for me.
Basically I want to create array[0]...array[1]..array[2] and so on and have a pointer that points to t...
The code below compiles, but immediately crashes for reasons obvious to others, but not to me. I can't seem to get it right, can anyone tell me how to fix this.
*array_ref[2] = array[0];
*array_ref[3] = array[1];
It crashes on that part everytime.
typedef struct test {
char *name;
char *last_name;
} person;
int setName(per...
what is the difference between "Strict", "Typed", "Restricted" and "Disjointed" aliasing?
...
Hi everyone !
I'm stuck with calling an external DLL and passing a function (pointer) as parameter.
I've recently had different problem of passing some arguments to DLL and you helped.
Hope, someone know how to do this as well....
Here's function declaration in DLL (cpp) that needs to be called from Delphi:
typedef void (*PTR_Alloca...