I'm trying to swap the data fields between nodes in my linked list, having trouble swapping the char arrays. this is just a sample of the program.
struct node {
int count;
char word[50];
struct node *next;
};
void swap_nodes( struct node *first, struct node *second ) {
int temp_count;
char *temp_word;
temp_count = first->count;
temp_word = first->word;
first->count = second->count;
first->word = second->word;
second->count = temp_count;
second->word = temp_word;
}
Let me know what I'm doing wrong, I'm very new at writing in c.
When you assign an array of characters to a pointer, you do not make a copy of the array:
char *temp_word;
temp_word = first->word;
temp_word points to the initial element of the array, so assigning to the array would change the data pointed to by the pointer as well.
You can fix this by declaring an array of 50 characters, and using strcpy or memcpy to do the copying:
char temp_word[50];
memcpy(temp_word, first->word, sizeof(temp_word));
memcpy(first->word, second->word, sizeof(temp_word));
memcpy(second->word, temp_word, sizeof(temp_word));
Well, you have already received answers, I just want to point out that you could exchange the nodes position in the list (instead the node content). As you have a single linked list, you will need the parents of the nodes to do so.
Alternatively, you can use dynamic memory instead the static array for "word", this way you only have to swap the pointers, avoiding the array copy.
The word[50] is a part of struct node, it's inside of the struct node, while what you have done is just move a pointer *temp_word point to *first, then to *second, the content of the word[50] is not changed in deeply. you can use memcpy to change the contents.
An appropriate implementation with strncpy and strdup would be:
#include <string.h>
void swap_nodes( struct node *first, struct node *second ) {
int temp_count;
char *temp_word;
temp_count = first->count;
temp_word = strdup (first->word);
first->count = second->count;
strncpy (first->word, second->word, 50); /* 50 based on struct definition */
second->count = temp_count; /* could be ( strlen (temp_word) + 1 ) */
strncpy (second->word, temp_word, 50);
if (temp_word) /* free memory allocated with strdup */
free (temp_word);
}
Related
I'm struggling to figure out how to pass a string into a function and then how to store it in struct via pointer. I want to create a linked list where each node contains a node name(string) and data(integer) representing the weight required to reach that node.
The structure representing the node is as follows:
struct ListNode
{
// The energy required to reach in this node
int data;
// The name of the node
char location[20];
// And the nodes along side this one
struct ListNode* next;
struct ListNode* prev;
};
The following function generates a node and sets its next and previous pointers:
// Allocate a new listNode with the provided value
struct ListNode* listNodeConstructor(int value, char *city)
{
struct ListNode* newNode;
// Reserve memory for a node
newNode = malloc(sizeof(struct ListNode));
// Set its values
newNode->data = value;
newNode->name = strdup(city); /* This is how I've tried to implement it but it causes an error */
newNode->next = NULL;
newNode->prev = NULL;
// And return it
return newNode;
}
If anyone can show me how to correctly store the string in the node struct, I would be eternally grateful.
strdup() copies a string to a newly malloced place in the heap and returns a pointer to the new string.
Note that you also need to free it.
The problem is, that the string you want to set is part of the structure and not just a pointer you can set.
You have two options:
Use strcpy(newNode->name,city); instead of newNode->name = strdup(city);. This copies the city string to newNode but you need to assure that city has a \0 until newNode->name overflows.
change name to be just a pointer and free it when you free the node. You can use strdup in that case. (Change char location[20]; to char *location;
You cant assign arrays. You can only assign scalar variables or structs or unions
struct ListNode
{
// The energy required to reach in this node
int data;
// The name of the node
char *name;
char name1[32];
struct ListNode* next;
struct ListNode* prev;
};
int foo(struct ListNode *node, const char *str)
{
node -> name = str; // but remember that it only assigns the
//reference and does not copy the string
/* OR */
node -> name = strdup(str); // but remember to free it
strcpy(node -> name1, str);
}
You are attempting to use strdup (3) which creates a copy of the string in a newly heap allocated space. You are therefore attempting to assign a pointer (which is the return of your strdup to a char array. As you already have allocated your string space in your structure, you should therefore use strcpy (3) instead in the following way: strcpy(newNode->name, city).
Also please note it is always a good practice to pass pointer parameters as const when you do not intend to modify them. This convention aims to improve readability and is very helpful when you want to debug your programs when they grow bigger.
typedef struct{
char id[15];
int count;
}hashtag;
typedef struct node{
hashtag *hashtag;
struct node*next;
}*link;
I'm writing a program to read hashtags from a sentence, and I want to store them in a list. I've already defined this two structures, and I can read and pass the hashtags to the function below but I need help allocating memory in order to copy the string to the list.
void check_insert(char newh[]){
link x;
//malloc to allocate memory for the string I want to copy
strcpy(x->hashtag->id, newh);
x->hashtag->count += 1;
head = insert_end(head, x->hashtag); //head is a global variable that points to the 1st element of the list
}
You should allocate and initialize the pointer x in check_insert, it is undefined behavior to dereference it and access its members without allocation first:
void check_insert(char newh[]){
link x = malloc(sizeof *x);
x->hashtag = malloc(sizeof *x->hashtag);
// strcpy(x->hashtag->id, newh); <-- UB if newh is larger than 14 in size
x->hashtag->id[0] = '\0';
strncat(x->hashtag->id, newh, sizeof(x->hashtag->id));
x->hashtag->count = 1;
head = insert_end(head, x->hashtag);
}
I am trying to write a function that can read some info from a file into a node in a doubly linked list. The format for the each nodes data is as follows.
struct(named record)
artist
album
song
genre
songLength(This is another struct that contains mins and secs)
playcount
rating
void load(FILE *file, Node *head)
{
char tempArtist='\0', tempAlbum='\0', tempTitle='\0', tempGenre='\0'
,tempSpace='\0',tempMins='\0',tempSecs='\0';
SongLength *tempLength=NULL;
int tempPlay=0, tempRating=0,test=0;
tempLength = (SongLength*)malloc(sizeof(SongLength));
fscanf(file,"%s",&tempArtist);
fscanf(file,"%s",&tempAlbum);
fscanf(file,"%s",&tempTitle);
fscanf(file,"%s",&tempGenre);
fscanf(file,"%s",&tempMins);
fscanf(file,"%s",&tempSecs);
fscanf(file,"%s",&tempPlay);
fscanf(file,"%s",&tempRating);
fscanf(file,"%s",&tempSpace);
tempLength->mins=tempMins;
tempLength->secs=tempSecs;
head->data->album=tempAlbum; // breaks here
head->data->artist=tempArtist;
head->data->genre=tempGenre;
head->data->song=tempTitle;
head->data->length=tempLength;
head->data->played=tempPlay;
head->data->rating=tempRating;
}
This is my current load function. When attempting to store these values in to nodes data I get an access violation.
Here are my structs for easy reproduction
typedef struct songlength
{
int mins;
int secs;
}SongLength;
typedef struct record
{
char artist;
char album;
char song;
char genre;
struct songlength *length;
int played;
int rating;
}Record;
typedef struct node
{
struct node *pPrev;
struct node *pNext;
struct record *data;
}Node;
makeNode
Node *makeNode(Record *newData)
{
Node *temp = NULL;
temp=(Node*)malloc(sizeof(Node));
temp->data=newData;
temp->pNext=NULL;
return temp;
}
If any confusion arises just let me know!
Also this is my first experience with dynamic memory so be gentle :P
Thanks!
These lines are not right.
fscanf(file,"%s",&tempArtist);
fscanf(file,"%s",&tempAlbum);
fscanf(file,"%s",&tempTitle);
fscanf(file,"%s",&tempGenre);
fscanf(file,"%s",&tempMins);
fscanf(file,"%s",&tempSecs);
fscanf(file,"%s",&tempPlay);
fscanf(file,"%s",&tempRating);
fscanf(file,"%s",&tempSpace);
They will definitely lead to undefined behavior because of the way the variables are defined.
You cannot expect
char c = '\0';
fscanf(file, "%s", &c);
to work. There isn't enough memory at &c to read a string. You need something like:
char s[100]; // Or some size that is large enough to hold the data
// you are about to read.
fscanf(file, "%99s", s); // Make sure that you don't read more than 99
// characters. Leave at least one character
// for the terminating null character.
I hope that gives you enough clues on how to change your variables.
You did not assign memory for the variable tempLength to point to.
Add this before accessing the elements
SongLength *tempLength = malloc(sizeof(struct(SongLength));
EDIT
I'm just giving an overall idea how to allocate and use nested structs for your case
Node *head;
Record *r=malloc(sizeof(struct record));
SongLength *s=malloc(sizeof(struct songlength));
r->length=s;//<----- 1
r->length->mins=10;//Now you can assign values
head=malloc(sizeof(struct node));
head->pPrev=NULL;
head->pNext=NULL;
head->data=r;//<--- The length member inside record is already assigned memory in 1
head->data->artist='c';
head->data->length->mins=10;//assign
I'm trying to create a linked list with a head and a tail node in C. Each node needs to hold an int and a string. My issue is that when I create a new node, assign it the correct values, and add it to the end of the list. All previous nodes obtain the string that I assigned the newest node. The int values stay correct, but its like their char pointer gets reassigned. I'm guessing I'm doing somethign wrong with pointers and have looked at tons of examples online and can't see where I'm going wrong. I included my node structure and add function. Thanks for all the help!!
// Linked list (queue) for requests
typedef struct request {
struct request *next;
int request_id;
char *command;
} request_t;
// Global variables
request_t *head = NULL;
request_t *tail = NULL;
void add(int r_id, char *c) {
request_t *node = NULL;
node = (request_t *)malloc(sizeof(request_t));
node->request_id = r_id;
node->command = c;
node->next = NULL;
if(head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
You need to create a duplicate of the string.
i.e. in the add function you require the line
node->command = strdup(c);
In addition you will have to free this string to prevent memory leaks when you free the node.
node->command = c;
Should be something like:
// Allocate enough memory for a copy of the string pointed to by c
node->command = malloc(strlen(c) + 1);
// Copy the string pointed to by c, to the newly allocated memory
strcpy(node->command, c);
Or since you have to call strlen on c you can use the more efficient:
size_t num_bytes = strlen(c) + 1;
// Allocate enough memory for a copy of the string pointed to by c
node->command = malloc(num_bytes);
// Copy num_bytes bytes from the memory pointed to by c, to the newly allocated memory
memcpy(node->command, c, num_bytes);
So that you make a copy of the string. Your delete function should be modified to free the allocated memory as well:
free(node->command);
So what was wrong with your code?
Without copying the string you just copy the pointer, so if you call add(10, somestr); Then somestr is a char * (a pointer). If you modify the string at that memory location it will also be modified in your linked list since there is really only one string with two pointers to it (command and somestr).
I never knew one could assign a string to another using a '=' operator :P This may work fine with structure variables but not with strings. Try using strcpy(node->command, c) after dynamically allocating size to node->command. This should work! :)
I have been at this problem for the last 6 hours and have been hitting google like mad to no avail.
Right I need a pointer to an array. This array contains pointers to Linked lists. Im going to have to malloc it since I dont know the array size until runtime.
LList **array
This was my first thought but this just gives me a pointer to an array of LList. Or atleast that is my understanding. Can someone give me a hand?
EDIT: Some info on how it would be used: I am implementing a very basic hash table. There is a structure that contains a pointer to an array of pointers to linked lists.
It needs to be a pointer to the array so that when I resize the table. I can just change the pointer to point to the larger table.
It sounds like you're on the right track.
LList **array;
array = malloc(num_ptrs * sizeof(LList*));
array is now an array of pointers to LList, and elements such as array[3] will be a pointer to a LList.
Arrays and pointers are very similar in C (but not identical!), as shown by the classic example: *(array + 2) is mostly equivalent to array[2].
Edit:
When you need to resize the table, you'll just need to realloc the additional space:
LList **new_array;
new_array = realloc(old_array, new_size * sizeof(LList*));
new_array and old_array may or may not be the same pointer afterwards, but either way new_array is guaranteed to be a pointer to enough space to hold the new array (or NULL if the memory couldn't be allocated)
2nd Edit:
As user411313 alluded, if you want the actual pointer to the array, you'll need to take the address of the array:
LList ***p_array;
p_array = &array;
A pointer to an object, is basically the same as a pointer to an array.
int * blah; // an int pointer. It could point to an array of ints, or a single int.
int ** blah; // a pointer to an int pointer. It could point to something that points to an int, or it could be pointing to an array of pointers to single ints, or it could be a pointer that points to an array of ints.
It all depends on how you use it.
A pointer to a pointer can also be an array of pointers.
int nLists; /* number of lists*/
LList **array;
array = (LList **)malloc(nLists * sizeof(LList *));
will make array be an array of pointers to LList. Then array[i] will give you the pointer to the i-th linked list in the array.
if you have to write your own linked list, you can do this.
typedef struct LLNode {
LLNode* next;
int data;
} LLNode;
LLNode* linkedList = null; // a linked list
LLNode** linkedListArray = (LLNode**) malloc( arraySize* sizeof(LLNode*) );
LLNode*** pointerToLListArray = &linkedListArray;
with a linked list library:
LList* linkedListArray = (LList*) malloc( arraySize* sizeof(LList) );
LList** pointerToLListArray = &linkedListArray;
typedef struct LList LList;
struct LList {
int value;
LList *next; };
LList *(*p)[3]; /* pointer to an array of 3 pointers to LList */
LList ll1 = {11};
LList ll2 = {22};
LList ll3 = {33};
size_t sizeofarray = sizeof*p/sizeof**p; /* calc arraysize at runtime here */
p = malloc( sizeofarray * sizeof**p ); /* allocate space for each LList-pointer in array */
(*p)[0] = &ll1;
(*p)[1] = &ll2;
(*p)[2] = &ll3;
/* test output here: */
printf("\n%d\n%d\n%d", ((*p)[0])->value,((*p)[1])->value,((*p)[2])->value);
free(p);