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.
Improve this question
BOOL test(LPTSTR a) {
char res[1024];
strcat(res,"before");
strcat(res,a); //<--
strcat(res,"after");
printf("%s",res);
}
How to concatenate LPTSTR with a char array?
Edit:
I have another functions which is returning a string in LPTSTR format. i cannot change this functions.
The problem is that the statement strcpy(res,a) does not compile. Says thats "LPTSTR" and "const char *" is not compatible.
Beside of this you are all right there are more problems with this snippet, but its not real just for showing the context of the strcpy statement.
Main problem is how to convert a lptstr to const char *, to can concatenate.
There are some issues in your code:
BOOL test(LPTSTR a) {
char res[1024];
strcat(res,"before"); // you're contatenating a string to res which is not
// initialized and which therefore contains garbage
strcat(res,a);
strcat(res,"after");
printf("%s",res);
} // test is declared to return a BOOL but you
// don't return anything, the compiler should warn you
You probably want this:
void test(LPTSTR a) {
char res[1024];
strcpy(res,"before");
strcat(res,a);
strcat(res,"after");
printf("%s",res);
}
Is there a reason why you use LPTSTR? Do you know what it is? The subject is quite vast. Probably you want to forget about LPTSTR and just use char*:
void test(char *a)
...
But without more information it's hard to tell.
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 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
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.