Solved

two dimensional strings.

Posted on 2004-10-05
4
234 Views
Last Modified: 2010-04-15
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
Comment
Question by:prashanth_gurijala
4 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 12235027
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
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 12235298
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12235635
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12238370
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
Inorder binary search tree 5 166
What is atomic operation? 6 59
How to access ANSI/IEEE Std 754 or equivalent information ? 3 64
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question