Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I have a structure and an array of such structures:
typedef struct clientInformation{
int inUse;
int socketNumberClient;
char *portNumber;
int listeningPort;
char *clientsName;
char *clientsIP;
}clientInformation;
clientInformation client[10];
I initialize the array by calling this function:
void addToList(char *ipaddress,char *p,char *cName,int socketNumber,int clientPortListen){
int i;
for(i=0;i<10;i++){
if(client[i].inUse==0){
client[i].inUse=1;
client[i].socketNumberClient=socketNumber;
client[i].listeningPort=clientPortListen;
client[i].portNumber=p;
client[i].clientsName=cName;
client[i].clientsIP=ipaddress;
break;
}
}
}
I am calling the initialization function function from the main() function, using this:
addToList(clientIP,clientPort,clientName,clientSocketNew,clientPortListen);
The problem is that I am unable to access the members of a structure in the array after initializing. I am unsure whether the members are populated or not.
For an instance when I try to print in main() this:
printf("%d",client[8].inUse);
It outputs nothing.
The comments under the question explained why the problem happened and how to solve it quite well. Sum it up here.
In order to get stdout flushed when you make your outputs, you need a \n at the end of the output.
Change
printf("%d",client[8].inUse);
To
printf("%d\n",client[8].inUse);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
First of all, this code is running without any issue on my linux desktop pc (x86_64) but on my Cyclone v (arm cpu/fpga), I have a segmentation fault because the value of the pointer is changing. The relevant line is the last one, during the for loop, the value of "layer->filename" is changing, it is correct during the first iteration (the address given by malloc) but it changes on the second one.
Basically, this bit of code is copying character from "buff" to "layer->filename", as you can see in the output file, the value of buff[i] is a valid character so it should not corrupt layer->filename.
If you have an idea of what could cause this issue, please let me know.
Thank you for your help.
typedef enum
{
CONV,
BN,
FC,
} layer_type;
typedef struct layer{
layer_type layer_type;
int shape[3];
char *filename;
} layer;
...
layer *layer=malloc(sizeof(layer));
char buff[30];
int i;
...
layer->filename = malloc(sizeof(char)*(n+1));
if(buff[0]=='b')
layer->layer_type=BN;
else if(buff[0]=='c')
layer->layer_type=CONV;
else
layer->layer_type=FC;
for(i=0; i<n ; i++)
*(layer->filename+i)=buff[i];
values of buff[i] and layer->name during the loop
Using this code
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
CONV,
BN,
FC,
} layer_type;
typedef struct layer{
layer_type layer_type;
int shape[3];
char *filename;
} layer;
size_t test(size_t x) {
printf("%d\n", (int)x);
return x;
}
int main(void) {
layer *layer=malloc(test(sizeof(layer)));
return 0;
}
You can see that sizeof(layer) in the line
layer *layer=malloc(sizeof(layer));
is not the size of a structure but the size of a pointer.
This is because the name of variable is the same as the type name and the compiler treated layer in sizeof as the variable (pointer) name.
To avoid this and have it allocate the size of structure, you should change the name of type or variable to avoid confusion.
Dereferincing the pointer
layer *layer=malloc(sizeof(*layer));
also can solve this problem, but I think renaming is better.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
im having a txt file
abc=123 def=456 ...
I have defined my struct like this:
typedef struct rule {
char* old;
char* new;
}Rule;
I have counted the ammount of these rules via function
int count_rules();
Now I'm calling this function in another, while making dictionary of rules
void make_dic(){
ammount_rules = count_rules();
//here goes the problem
Rule *dictionary = malloc(ammount_rules * sizeof(Rule));
}
I want to scan another txt and replace old with new so I'd like to acces every twin with simple command
for (i=0; i<ammount_rules;i++){
if ( (string_in_text) == (dictionary.old[i]) )
{
printf("%s" dictionary.new[i]);
}
}
Your malloc seems to be fine. It allocates an array of Rule with ammount_rules elements.
However, the way you use dictionary seems wrong.
dictionary.old[i] // wrong
dictionary[i].old // correct
dictionary.new[i] // wrong
dictionary[i].new // correct
BTW: Notice that you are comparing pointers - not strings. Use strcmp for comparing strings.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I have a some problems writing a code in which I want to modify a file extension stored in a string.For example string bla/bla/file.icc i want to be changed to bla/bla/file.cmr. This string makes part from a structure. I have 2 issues. One is that strcpy gives this message "expected expression before td_ActDOR and second one is in for and give's this message subscribed value is neither array nor pointer.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_ActDOR
{
char pDOR_file[86];
}td_ActDOR;
int main(void)
{
char path[80]="blabla/blabla/aici.icc";
td_ActDOR *Obiect;
Obiect = (td_ActDOR *)malloc(sizeof (td_ActDOR));
strcpy(td_ActDOR->pDOR_file, "blabla/blabla/file.icc");
int path_lenght=strlen(td_ActDOR->pDOR_file);
int i;
char bla[4] = "rmc\0";
printf("Stringul before: %s\n",path);
for (i = 0; i < 3; i++)
{
Obiect->pDOR_file[path_lenght-(i+1)] = bla[i];
}
printf("Stringul after: %s\n",path);
return 0;
}
In your code, td_ActDOR is not a variable, (it's a type), Obiect is.
Change
strcpy(td_ActDOR->pDOR_file, "blabla/blabla/file.icc");
to
strcpy(Obiect->pDOR_file, "blabla/blabla/file.icc");
Same goes for strlen(td_ActDOR->pDOR_file);, too.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
How do I dynamically create an array of struct in a function in C?
The struct:
typedef struct Track {
char artist[LONGSTR];
char file[LONGSTR];
int id;
int isAlbum;
char name[LONGSTR];
int pos;
char title[LONGSTR];
int time;
} Track;
The function:
int
processplaylist (struct Track** tracks, char* resp)
{
//count the tracks to return
//allocate space for the resulting tracks
*tracks = mem_alloc (count * sizeof (struct Track));
//process each track
return count;
}
And the usage:
char playliststr[] = "file: some-mp3\nTitle: Dire Straits - Romeo And Juliet\nName: TheRadio\nPos: 0\nId: 12\nOK\n"
struct Track* tracks = NULL;
int count = mpd_processplaylist(&tracks, playliststr);
Within the function the tracks are nicely processed and upto the return statement tracks points to the right location to get to the tracks. Most questions I have seen are about arrays to values, not structs.
This answer returns an array of structs, but I would like to return the count (question of style) and return the array through the parameter.
What is going wrong? After the function returns, count has the right value and tracks is still NULL.
As the not so nice people pointed out by down voting the question, the question is too broad and vague. I apologize for that, I was desperate.
As the nice people in the comments confirmed, the code is correct. Even though the debugger showed that just before the return statement everything was OK and after returning to the usage it was not.
By commenting out code lines and logging a lot, there are two things to note.
Within the function, you must refer to the individual tracks as (*tracks + i) or, for example, (*(*tracks + i)).file. Indeed, not shown in the sample code. I had tried *(tracks + i), &tracks[i] and tracks + i, which in the debugger all seemed to work and did not cause immediate errors. I guess the code messed up memory that only came out after the return.
Outside the function, in the usage, you refer to the tracks as an array, as tracks[i] or, for example, tracks[i].file.
I hope that at least my answer helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code: array of structures that must be sorted. Program works, but:
I can't understand, if Mat is pointer, why not void sort(tArt *sMat), but void sort(tArt sMat[]). I'm really puzzled.
typedef struct{
char data[26];
}tArt;
...
int main(void)
{
FILE* fMat; fMat=fopen..........
tArt* Mat;
...
Mat=malloc(sizeof(tArt));
for(i=0;i<N;i++) fread(&Mat[i],sizeof(tArt),1,fMat);
fclose(fMat);
sort(Mat,N);
...
}
void sort(tArt sMat, int num) {...........}
My guess since we can not see the entire code, is that when you use malloc to dynamically allocate the array you forget to allocate the array for N 'objects'. In other words, I suspect your problem lies in the line
Mat = malloc(sizeof(tArt));
where it should be
Mat = malloc(sizeof(tArt) * N);
On the other hand, when you create explicitly your array with a declaration of the form
tArt Mat[N];
where N is defined somewhere earlier in the ellipses, everything is working as expected.
Hope this helps.
There is no difference between tArt *sMat and tArt sMat[] and tArt sMat[1234], the compiler treat them all as tArt *sMat and ignore the length information.