hello friends,
I am having a problem.
This code works just fine when I print as soon as I assign the string to
the pointer.
Code:
ptr->main = malloc(sizeof(detail)*(660)+1);
strcpy(ptr->main[count].key,token[i]);
printf("key key %d %s ",count, ptr->main[count].key);But when I try to do this.... -
for(i=0; i < count; i++)
-
{
-
printf("%d %s %s \n", i,ptr->main[count].key);
-
}It doesnt work.
-
What am I doing wrong in the for loop...
-
-
-
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
in main{
-
FILE *data_txt,
-
struct ARRAY1 *str_ptr;
-
struct ARR2 *new_ptr;
-
str_ptr = &ARRAY1_info;
-
new_ptr = &ARR2_info;
-
-
struct first *ptr;
-
struct second *new;
-
-
data_txt = fopen("data.txt","r");
-
-
while (fgets(line,100,data_txt)!=NULL)
-
{
-
-
-
-
trim(line);
-
strcpy(first_info.name,"ccmlmd");
-
if ((strlen(line) > 1)&&(read!=0)){
-
lcount++;
-
tokens = split(line, delim);
-
for(i = 0; tokens[i] != NULL; i++) {
-
if (i==0){
-
if (tokens[i]!=NULL){
-
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].key,token[i]);
-
printf("key key %d %s ",count, ptr->main[count].key);
-
}
-
}
-
-
}
-
for(i = 0; tokens[i] != NULL; i++)
-
free(tokens[i]);
-
free(tokens);
-
count++;
-
lcount++;
-
}
-
-
// This doesnt work
-
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
16 3404
Question moved to C / C++ Forum.
In what way does it not work? If it gives you compiler errors, please post them here. If it gives you the wrong output, please post it here, along with the expected output. This will make it much easier for people to help you. Thanks.
Basically this doesnt print anything -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
Have you tried debugging it? What does printf return to you?
The printf in the for loop prints only this
But when further up in the code prints just fine. -
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].key,token[i]);
-
printf("key key %d %s ",count, ptr->main[count].key);
-
This prints just fine.
Only problem is that this part of the code doesnt work -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
what type of data is ptr->main[i].time and ptr->main[i].date?
As I posted above -
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
Thats the info I am trying to catch
It's hard for me to tell what's wrong with your program because the code you posted has typos and does not compile. Two problems I can see are that in your posted code, you have not initialized ptr->main[i].time or ptr->main[i].date, and the line you posted separately, - printf("%d %s %s \n", i,ptr->main[count].key);
has one too many %s, but I don't know if these things are causing problems for you.
Oh ok...But since I have already initialized the pointer further up...wouldnt that be enough...or do I have to initialize the ptr again in this part of the code...? -
for(i=0; i < count; i++) {
-
printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
-
}
-
You do not initialize the date or time members in your code. You do initialize
but the ARRAY1 struct does not have a member called key. I'm afraid the code you posted does not make any sense.
I am sorry, please subsitite the 'key' and 'value' fields with 'time' and 'date'.
Instead of making us substitute everything and try to force your code to work, why don't you provide a listing of your code that compiles so we can see what you are doing.
OK here is the corrected code. -
typedef structn ARRAY1{
-
char time[ARRAY_SIZE];
-
char date[ARRAY_SIZE];
-
}ARRAY1,ARR2;
-
-
typedef struct first{
-
char name[10];
-
ARRAY1 *main;
-
}first;
-
-
typedef struct second{
-
char db[10];
-
ARRAY2 *other;
-
}second;
-
in main{
-
FILE *data_txt,
-
struct ARRAY1 *str_ptr;
-
struct ARR2 *new_ptr;
-
str_ptr = &ARRAY1_info;
-
new_ptr = &ARR2_info;
-
-
struct first *ptr;
-
struct second *new;
-
-
data_txt = fopen("data.txt","r");
-
-
while (fgets(line,100,data_txt)!=NULL)
-
{
-
-
-
-
trim(line);
-
strcpy(first_info.name,"ccmlmd");
-
if ((strlen(line) > 1)&&(read!=0)){
-
lcount++;
-
tokens = split(line, delim);
-
for(i = 0; tokens[i] != NULL; i++) {
-
if (i==0){
-
if (tokens[i]!=NULL){
-
ptr->main = malloc(sizeof(detail)*(660)+1);
-
strcpy(ptr->main[count].time,token[i]);
-
printf("time time %d %s ",count, ptr->main[count].time);
-
}
-
}
-
-
}
-
for(i = 0; tokens[i] != NULL; i++)
-
free(tokens[i]);
-
free(tokens);
-
count++;
-
lcount++;
-
}
-
-
// This doesnt work
-
for(i=0; i < count; i++) {
-
printf("%d %s ", i,ptr->main[i].time);
-
}
-
@crumbs12
If it doesn't print anything, what is value of 'count'?
The code in post #14 is incomplete and contains typos ...
Line 1: "struct", not "structn".
Lines 2 and 3: ARRAY_SIZE is undefined.
Line 16: "int", not "in".
Line 17: this implies that you included <stdio.h>.
Lines 20 and 21: ARRAY1_info and ARR2_info are undefined.
Line 26: you don't check if fopen fails.
Line 28: "line" is undefined.
Line 33: function "trim" is undefined.
Line 34: this implies that you included <string.h>.
Line 34: "first_info" is undefined.
Line 35: "read" is undefined and uninitialized.
Line 36: "lcount" is undefined and uninitialized.
Line 37: "tokens" is undefined.
Line 37: function "split" is undefined.
Line 37: "delim" is undefined and uninitialized.
Line 38: "i" is undefined.
Line 41: "ptr" is uninitialized.
Line 41: "detail" is undefined.
Line 41: this implies that you included <stdlib.h>.
Line 41: you don't check if malloc fails.
Line 42: "count" is undefined and uninitialized.
Line 42: "tokens", not "token" ... I think.
Lines 49 and 50: you didn't malloc either of these, but you're freeing them. I suppose trim() must have malloc'd them.
Line 57: ptr->main was malloc'd at line 41, are you sure it wasn't also free'd by lines 49 or 50?
These aren't errors, but could be cause for confusion:
Lines 1 and 4; 6 and 9; 11 and 14: the struct name is the same as the typedef name. Some people prefer to do it like this, others prefer to avoid all ambiguity.
Line 8: "main" is typically pseudo-reserved for the name of the main function.
Lines 18, 19, 23, and 24: after going to the trouble of using typedefs you ended up using the structure declarations.
These comments are based on code inspection. Additional problems might come to light if I tried to compile the code.
If you don't want to copy your code entirely to the site use a service like Free File Hosting Made Simple - MediaFire to upload a zipped copy of your code for others on this site to inspect. Make sure you link to it properly here.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
I had some code that worked fine for several weeks, and then yesterday
it stopped working. I'm not sure what I did. Nor can I make out why it
isn't working. I'm running a query that should return 3...
|
by: lawrence |
last post by:
I posted before, but have now narrowed my problem down to this method.
At the start of the method, I test to make sure that I have a
resource, a pointer to data returned from a database. This test...
|
by: lokman |
last post by:
Hi,
In the following code, can someone tell me the difference between *p++ and
p++ ? I can see both achieve the same result.
Thanks a lot !
|
by: Fatted |
last post by:
I'm trying to learn how to create arrays dynamically. But its just not
happening. Have a look at code below and point and laugh where
appropriate...
First part of program, I'm using an array of...
|
by: Meenu |
last post by:
Hi,
Can two different far pointers contain two different addresses but
refer to the same location in memory?
|
by: Martin Jørgensen |
last post by:
Hi,
I don't understand these errors I get:
g++ Persort.cpp
Persort.cpp: In function 'int main()':
Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping
Persort.cpp:37:...
|
by: Jacek Dziedzic |
last post by:
Hi!
I'm trying to squeeze a few clock cycles from a tight
loop that profiling shows to be a bottleneck in my program.
I'm at a point where the only thing that matters is
execution speed, not...
|
by: code break |
last post by:
Hi all,
What is the difference between stack pointer and frame pointer ?
Any suggestions are welcome ,,,
|
by: Bernd Gaertner |
last post by:
Dear experts,
according to my interpretation of the Standard, loop 1 in the
following program is legal, while loop 2 is not (see explanation
below). This looks a bit counterintuitive, though; do...
|
by: shaun roe |
last post by:
Question about pointer-to-data members
I have a deeply nested loop, the innermost bit looks a little like this:
for(it(vec.begin(), end(vec.end()), it!=end;++it){
if (global == 0){
myObj =...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |