strcpy() with copying string from one structure to another [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
it seems that this is a duplicate question, but I searched stackoverflow's question about that point and non is like to my problem(I think)
I've two variables of a struct each has its own pointer to char, when I tried to copy from one variable's string to another variable's string, nothing happened, although no errors appear, just warning
implicit declaration of function strcpy
incompatible implicit declaration of built-in function 'strcpy'
I read from some questions on stackoverflow that you'd better to use strdup() function instead of strcpy() but when I did, I had an error
too many arguments to function 'strdup'
I read that there's a problem with strcpy() called "segmentation fault" and I knew it's about memory allocation, I don't totally understand what it's exactly and don't know if it's the problem with my code?
and this is my code
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
strdup(q.name,p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}
so what is the problem and how I can solve it?

strdup() takes only one argument; it malloc's and returns a new block of heap memory containing the duplicated string. (See https://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c)
Which probably also points to the problem you were having before -- were you remembering to malloc the space for q to copy p's contents into?

change
strdup(q.name,p.name);
to
q.name = strdup(p.name);
see man page of strdup for further details. The strdup() function returns a pointer to a new string.
full code:
#include <stdio.h>
#include <string.h>
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
q.name = strdup(p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}

Related

Trouble with C pointers when writing a linked list implementation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I've been getting back into C programming recently and have some issues with a linked list implementation I'm writing.
Here are my structure definitions:
typedef struct linked_list_entry {
struct linked_list_entry *next;
struct linked_list_entry *prev;
void* data;
} linked_list_entry;
typedef struct linked_list {
uint32 count;
struct linked_list_entry *first;
struct linked_list_entry *last;
} linked_list;
Here's the code in question:
int linked_list_add_entry(linked_list** linked_list, void* data)
{
linked_list_entry* new = malloc(sizeof(linked_list_entry));
...
DPRINT("last(%p), new (%p)\n", (*linked_list)->last, new);
(*linked_list)->last->next = new;
DPRINT("(*linked_list)->last(%p)->next (%p)\n", (*linked_list)->last, (*linked_list)->last->next);
With debug output:
data-types/linked-list.c:37:linked_list_add_entry(): last(0x7fa497402790), new (0x7fa4974027b0)
data-types/linked-list.c:39:linked_list_add_entry(): (*linked_list)->last(0x7fa4974027b0)->next (0x0)
Any idea why the last pointer changes to new and the new pointer changes to NULL?
Thanks
The dump you provided shows that the value of (*linked_list)->last has changed after you did
(*linked_list)->last->next = new;
But this line of code does not change (*linked_list)->last (at least, it is not supposed to).
Possible explanations for such "magical" behavior include
**linked_list is not properly allocated. Too little memory is allocated for
**linked_list object. As a consequence, *(*linked_list)->last happens to overlap the memory location of **linked_list. This is why the above assignment appears to change (*linked_list)->last as well. I.e. due to the aforementioned pointer problem (*linked_list)->last and (*linked_list)->last->next occupy the same location in memory.
Things like that can happen, for example, when incorrect (insufficient) amount of memory gets allocated for an object. In that case the problem is in the calling code. It supplies a "broken" list as input.
How does the calling code allocate memory for linked_list itself? Note that in this function you used name linked_list for function parameter, thus hiding the name of the type linked_list. If you did something like that in the "other" function - the one that allocated linked_list object itself - then trying to use sizeof(linked_list) in there would have returned incorrect value (size of pointer instead of side of struct type). That would explain underallocation of memory.
(*linked_list)->last is not a properly initialized pointer. It points into some unpredictable location. As a consequence, *(*linked_list)->last happens to overlap the memory location of **linked_list etc etc etc (see above).
The code you were running is not the code you posted here.
The answer is that I needed to use the struct keyword when using malloc:
linked_list_entry* new = malloc(sizeof(struct linked_list_entry));

Getting error when trying to malloc memory to pointer variable declared separately [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.

Concatenating Strings in C Implicit Definition [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a question regarding concatenating two strings in C to say "Ring Ding"
I have one char * d which I malloc 14*sizeof(char) just to stay on the safe side to include the \o characters at the end, but my function is still segfaulting, and I'm not sure why. It says I cannot implicitly define strcopy. I'm thinking my problem is that I can't just come out and say "Ring", but I could be mistaken.
char * k = malloc(14*sizeof(char));
strcopy(k, "Ring");
strcat(k, "Ding");
printf("%s\n", k);
There is a typo for 'strcopy'; it is usually 'strcpy' from string.h
As C allows you to call functions without declaring them first, you got the warning for the 'Implicit declaration' as it was not found int string.h.
I am surprised you were able to run the program as you should have got a linker error as it could not find the definition of the function.
If you fix the typo, it should compile and run fine.
I would also recommend to use the strl* versions of these string functions (strlcpy, strlcat etc..) - it is much safer; see the man page.
Suppose you have:
char* str1 = "Ring";
char* str2 = "Ding";
Follow these steps (as a general rule):
// Function 'strlen' always returns the length excluding the '\0' character at the end
// The '+1' is because you always need room for an additional '\0' character at the end
char* k = (char*)malloc(strlen(str1)+strlen(str2)+1);
// Write "Ring\0" into the memory pointed by 'k'
strcpy(k,str1);
// Write "Ding\0" into the memory pointed by 'k', starting from the '\0' character
strcat(k,str2);
// Print the memory pointed by 'k' until the '\0' character ("RingDing") to the console
printf("%s\n",k);
// De-allocate the memory pointed by 'k'
free(k);
I hope this code help you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char * k = (char*)malloc(14*sizeof(char));
strcpy(k, "Ring");
strcat(k, "Ding");
printf("%s\n", k);
return 0;
}

Casting int to char* [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I can cast int to char but I need cast int to char*.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
char ch_number_of_rev[4];
version = "0.";
itoa(number_of_revision,ch_number_of_rev,10);
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
but I need cast int to char*
Casting only changes the type - it does not change the value within the variable. If you need to convert an int to array of chars (i.e. a string) then use sprintf or snprintf:
char* buffer = ... allocate a buffer ...
int value = 970;
sprintf(buffer, "%d", value);
Converting int to string in c
Also, you have not allocated any memory for version - use malloc and allocate some memory.
strcat here won't work because you haven't allocated any space to store the result in. Your version is probably in read-only memory anyway, so you'd get a segfault, otherwise you'll get memory corruption. So make sure to allocate enough space for it, e.g. by using
char version[10] = "0.";
You may want to read up on pointers first, though.

function pointer C programming [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm not sure what the following does and i'm hoping someone can clarify the purpose of having the asterisk in front of the functions name:
char *Foo(char *ptr) {
return NULL;
}
I understand that you can pass by value the memory location of something in the function argument call and *ptr would be the pointer to it. I understand you can create a pointer function that can be used to point to other functions like a regular pointer points to variable memory location but in this case this is not a function pointer that we can point to other functions, or is it? This seems like a real function.
Foo is a function.
It has input: ptr of type char*
It has output of type char*
char* means "pointer to char"
it returns NULL.
That is the most plain explanation I can think of.
its misleading you, the * by the name isn't related to the name
it means the same as char* Foo(char* ptr)
which means a function which takes a char* and returns a char*

Resources