[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

two dimensional strings.

Hi ,

 I have a structure with array of strings as a field as follows :

 struct transaction
 {
   char *special case;
   .........
  .....
};
transaction trans[20];
  and a global variable called opr which is a char array
char opr[10]="";

Now, this opr gets a line from a file each time a file read is executed and if this line satisfies a special condition then it is stored in the special case field of transaction.

Here is what i did.,
I tested the condition and then
trans[i].specialcase[a]=opr;

where  a is a counter.

Problem is the address of the specialcase[a] is being assigned to opr so that whenever opr gets a new value the specialcase[a] is pointing to that... which means the address of opr and specialcase[a] becomes the same...  I have to get rid of this problem.

I have to only store that special case and the address of both variables should be different. How to do that... Urgent please.
0
prashanth_gurijala
Asked:
prashanth_gurijala
1 Solution
 
lwinkenbCommented:
You dont have an array of strings here, you have an array of pointers.  You need to either change your struct to:

struct transaction
 {
   char specialcasebuff[SOMEBUFFERSIZE];
   .........
  .....
};

or you need to allocate memory to the pointers inside the transaction structures.
trans[i].specialcase = new char[SOMEBUFFERSIZE];

Next, to copy a string into these buffers, you cannot use a equals sign like you had:
trans[i].specialcase[a]=opr;
This only copies the pointer value like you noted in your post.  Instead, use the strcpy() function.
strcpy(trans[i].specialcase,opr);
0
 
PaulCaswellCommented:
If you have ONE special case per transaction.

Change your structure to:

struct transaction
 {
   char special case[10];
   .........
  .....
};

Then, where you detect the special case:

strcpy(trans[i].specialcase,opr);

This would copy the special case string into the transaction.
--------------------------------------------------------------------
If you have MANY special cases per transaction.

Change your structure to:

struct transaction
 {
   char special case[100][10];
   .........
  .....
};

Then, where you detect the special case:

strcpy(trans[i].specialcase[a],opr);

--------------------------------------------------------------------
I can think of other interpretations of your question. If the above doesnt help, please give more details.

Paul
0
 
brettmjohnsonCommented:
Either make specialcase an array of char (as Paul suggests),
or make a duplicate of the string in opr:

trans[i].specialcase = strdup(opr);

Just remember to free() the allocated memory when you are through with it.

I can't figure out what you are attempting to do with the index, a , in trans[i].specialcase[a]
specialcase is not an array of strings, it is a pointer to a single string.
(A string is a pointer to char [array of char implied]).


0
 
ankuratvbCommented:
If you have multiple special cases in a transaction,
I'd suggest a variant of what Paul suggested,

Instead of:
char opr[10];

use:

char opr[100][10];

and use char *specialcase; in the struct,

Now you have all your lines read in from the file stored separately in opr[i],you can make your special case pointer point to any line since you have allocated separate memory for each line.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now