Modify a string from a stucture [closed] - c

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.

Related

What could cause this pointer to be corrupted? [closed]

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.

Malloc in array gives Segmentation fault [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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.
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.
Improve this question
In my attempt to understand malloc and structs I have come across an error I do not understand
#include <stdio.h>
#include <stdlib.h>
typedef struct match
{
int round;
} match;
void foo(match *matches) {
for(int i = 0; i < 10; i++) {
matches = (match *) realloc(matches, i + 1);
matches[i].round = i + 1;
}
}
int main()
{
match *matches;
matches = (match *) malloc(0);
foo(matches);
free(matches);
return(0);
}
So in my attempt to fill this array of matches dynamicaly it fails
Your foo function is very flawed. First, the parameter passes a copy of the matches pointer, so when you realloc, that updates the foo matches pointer, but not the matches pointer in main. This may cause problems with the free in main. You need to change the parameter to foo to be a double pointer: void foo(match **matches). Then to realloc, *matches = realloc(...
Next, the second parameter to realloc is a size. But i + 1 isn't going to be big enough for a full copy of the match struct. You probably meant to do something like sizeof(struct match) * (i + 1).
I addition to above answer. Good Explanation...
Please check the error from realloc as well before using the memory,
Modified the program
void foo(match **matches) {
for(int i = 0; i < 10; i++) {
*matches = realloc(*matches, (i+1) * sizeof(match));
...
}
}
int main()
{
...
foo(&matches);
...
}

Converting individual pieces of assembly to machine code [closed]

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 am trying to convert assembly to machine code. I have a MINGW compiler where if i type ./convert.exe mov %a then it should output 0x01 0xc0. I am thinking of using a struct listing each assembly code with its corresponding machine value. At the moment i keep getting errors like "request for member opcode in something not a structure". Any help would be appreciated.
#include <stdio.h>
#include <string.h>
struct _Instruction {
char mnemonic[10];
unsigned char opcode;} typedef Instruction;
Instruction instruction_list[] = {
{"mov", 0x01},
{"add", 0x04},
{"sub", 0x05},
{"mul",0x06},
{"div", 0x07},
{"and",0x08},
{"or",0x09},
{"xor",0x0a},
{"cmp",0x0b},
{"",-1},
};
Instruction get_inst(char mnemonic[]);
int main2(int argc, char *argv[])
{
char* instruction = argv[1];
Instruction get_inst = get_Instruction(instruction);
printf("%s ; %s",instruction_list.mnemonic,instruction_list.opcode);
return 0;
}
Instruction get_inst(char mnemonic[])
{
int i;
for(i=0; instruction_list[i].opcode != -1; i++)
{
if(!strcmp(instruction_list[i].mnemonic, mnemonic))
{
return instruction_list[i];
}
}
return instruction_list[i];
}
For one thing, your structure is declared incorrectly. You should format it like this instead:
typedef struct _Instruction {
....
} Instruction;
I'm not sure why that's not triggering syntax errors, but it's certainly not helping.
Also, you have both a variable and a function named get_inst. You call a non-existent function named get_Instruction(). You probably meant to name your function get_Instruction().
Also, the .opcode member of your structure is a single char. Your printf statement uses the "%s" format specifier to print it. This expects a string, which will cause printf to continue reading past the end of the .opcode member, displaying unpredictable garbage and accessing memory it shouldn't be touching.

How to access this struct array in C function? [closed]

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);

Trying to write a simple structure base piece of code ...not getting? [closed]

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 8 years ago.
Improve this question
sorry i think i really messed up in few line of code based on structures...as i am new and last few days trying hard to understand C. Please check following code and guide me where i am wrong...thanks!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct family{
char name[20];
int age;
char father[20];
char mother[20];
};
//Function to compares two strings and returns 1 or 0
char siblings(struct family member1, struct family member2)
{
if(strcmp(member1.mother, member2.mother)==0)
return 1;
else
return 0;
}
int main()
{
//Following structure variables are decleared
struct family member1;
struct family member2;
//structure variables initilized with a string
member1.mother = "Rosy";
member2.mother = "Rosy";
//This function compares two strings and returns 1 or 0
siblings(member1.mother, member2.mother);
//trying to print resulst with respect to return from function
printf("%S\n",siblings(member1.mother, member2.mother)?"yes":"No");
system("PAUSE");
return 0;
}
Replace the following:
1-1: %S should be replaced to %d
printf("%S\n",siblings(member1.mother, member2.mother)?"yes":"No");
1-2: It'd make sense better to return bool or int if you're returning either 0 or 1.
char siblings(struct family member1, struct family member2)
2: "PAUSE" should be "pause"
system("PAUSE");
3: use strcpy for the below.
member1.mother = "Rosy";
member2.mother = "Rosy";
Replace
member1.mother = "Rosy";
member2.mother = "Rosy";
with
strcpy(member1.mother, "Rosy");
strcpy(member2.mother, "Rosy");
This is because the member mother is not a pointer, it is an array
EDIT
The call to siblings should be siblings(member1, member2) not siblings(member1.mother, member2.mother)

Resources