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.
Related
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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
This is just a part of the code and it is simplified for this question.
I would like to be able to store the word into my struct, the whole word not just the first char.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
int main(void) {
char a[]="";
a[0]='w';
a[1]='h';
a[2]='i';
a[3]='l';
a[4]='e';
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
}
this only prints w and I need it to print while
I cannot use anything other than the char a[]="";
a must be build in this way
When I print result it is while but when I put inside the struct it is only w
By doing struct lexics rs = {WHILE_KEYWORD, *a};, you are effectively putting the first element of a inside of the lexics.lexeme array. Try using a function that copies the entire array:
struct lexics rs = {WHILE_KEYWORD};
strncpy(rs.lexeme, a, LEXEME_MAX - 1);
rs.lexeme[LEXEME_MAX - 1] = 0; // to be sure that the string is properly terminated :)
In your code
char result[]="";
does not give you an infinite-length array, it's a one element array (only null-terminator). You need to either have a proper size mentioned, or a long-enough initializer to have a proper size of the array.
struct lexics{
enum token token;
char lexeme[LEXEME_MAX];
};
...
struct lexics rs={WHILE_KEYWORD,*a};
printf("%s\n",rs.lexeme);
In C arrays are filled with zero if there are less initializers than elements in the array.
In your code you only provide 1 single char to initialize char lexeme[LEXEME_MAX] array. This means the array looks like "w\0\0\0\0..." and as a result only the first character is printed.
You must use strcpy or strncpy to copy the string from array a into the array rs.lexeme.
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 5 years ago.
Improve this question
Could also be called a 3D array, as each string is essentially it's own array.
Would be in the following format, each row = a new 'order'.
[ [FirstName, SecondName, DOB, Newspaper] ]
[ [FirstName, SecondName, DOB, Newspaper] ]
The contents of the array will be all strings (even though DOB would be in the format "23012017" and then converted to integer if necessary
Tried to use this "pointer arrays", but am not sure about how to use it.
char *bookings[][2];
char firstname[20], secondname[20], dob[8];
char *bookings[][0]=firstname, *bookings[][1]=secondname, *bookings[][2]=dob;
I think what you need is a struct and a normal 1D array. Like:
struct order {
char FirstName[42];
char SecondName[42];
char DOB[42];
char Newspaper[42];
}
and in your code (e.g. in main)
struct order[42];
Then you do:
strcpy(order[0].FirstName, "Donald");
strcpy(order[0].SecondName, "Duck");
... and so on
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);
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.
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
DIR * d;
int dsize=0;
struct dirent * de;
char *dir[1024];
d=opendir(".");
while ((de = readdir(d)))
{
if((de->d_type) & DT_DIR)
{
dir[dsize]= de->d_name;
dsize++;
}
}
I'm trying to store the address of the file names into a array of char pointers.
A bit rusty on pointers I went back and read some pages of pointer review but I'm
not sure what I'm doing wrong.. Keeps telling me "warning: assignment makes integer from pointer without a cast". Is my syntax just off because of the struct?
You cannot store the pointers that way. They are overwritten every time, you call readdir and then you have a dangling pointer to invalid memory. If you want to store the dir entries, you must copy the whole name, not just the pointer
char dir[1024][256];
while (de = readdir(d)) {
if (de->d_type & DT_DIR) {
if (dsize < 1024) {
strcpy(dir[dsize], de->d_name);
dsize++;
}
}
}
Don't forget the check for the dir array bounds. Otherwise you risk overwriting the stack, which might result in a crash.