Thread: pointer printing

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    27

    pointer printing

    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....
    Code:
          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...

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    In the loop you are passing 2 variables to printf, but you seem to indicate that you want to print 3?

    Code:
    printf("%d %s %s \n", i,ptr->main[count].key);

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Sorry I meant to use only 1 %s...
    Code:
          for(i=0; i < count; i++)
       {
              printf("%d %s  \n", i,ptr->main[count].key);
          }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest (compilable) code that demonstrates the problem. You should also try and describe the problem more clearly than just "It doesnt work."

    For one thing, I notice that you write ptr->main[count] instead of ptr->main[i].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    hi,
    Here is the original struct
    Code:
    typedef struct{
            char time[ARRAY_SIZE];
            char date[ARRAY_SIZE];
    }ARRAY1, 2_ARRAY;
     
    typedef struct{
          char name[10];
           ARRAY1 *main;
       }first;
    When I print like below it prints fine
    Code:
     ptr->main = malloc(sizeof(ARRAY1)*(660)+1);
            strcpy(ptr->main[count].key,token[i]);
          printf("key key %d %s ",count, ptr->main[count].time);
    free (ptr)
    But when I print like below it core dumps
    Code:
         for(i=0; i < count; i++)
       {
              printf("%d %s  \n", i,ptr->main[i].key);
          }
    What am I doing wron in the for loop ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bandal27
    Here is the original struct
    You must be mistaken. You are using typedef to make ARRAY1 and 2_ARRAY the names of an anonymous struct. But that is not possible since the naming rules do not allow a name to begin with a digit such as 2.

    Quote Originally Posted by bandal27
    When I print like below it prints fine
    You must be mistaken. That code snippet should not even compile since there is a missing terminating semi-colon after the call to free(). More importantly, it is unknown as to what is ptr->main, count, token, and i, among other things.

    I say again: post the smallest and simplest compilable code that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    Hi,
    I cant copy the whole compiled code ...
    but can you see any problem in this code.

    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];
            2_ARRAY *other;
        }second;
    in main{
            struct ARRAY1 *str_ptr;   
            struct ARR2 *new_ptr;
          str_ptr = &ARRAY1_info;
          new_ptr = &ARR2_info;
     
           struct first *ptr;
           struct second *new;
    
    
    
     while (fgets(line,100,data_txt)!=NULL)
     {
            data_txt = fopen("data.txt","r");
    
    
    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);
          }
    I am particularly not able to print here.
    Code:
    // This doesnt work
          for(i=0; i < count; i++) {
     printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
          }
    If you can see anything wrong in my the last part of the code ....let me know

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think Laserlight made it pretty clear that you need to strip your code down to something that compiles and shows the problem.

    Code:
    in main{
    That line won't compile, for sure.

    Code:
     while (fgets(line,100,data_txt)!=NULL)
     {
            data_txt = fopen("data.txt","r");
    looks pretty much back to front - first you read from data_txt, then open it?

    Code:
    strcpy(ptr->main[count].key, ...)
    Won't compile, as there is no field "key" in ARRAY1.

    Code:
           2_ARRAY *other;
    Won't compile, as names must start with non-numeric character (a-z,A-Z,_).

    So how you can say that a certain line in that code won't work when there is no chance that it will compile, I don't know.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    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].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);
          }

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    I have made a few minor syntax errors while trying to explain the problem to you guys....please over look it :-)

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bandal27
    I have made a few minor syntax errors while trying to explain the problem to you guys....please over look it :-)
    Yes, some syntax errors can be spotted and fixed, but as it stands your example code is impossible to save. One of the critical parts that might cause a problem is the value and type of count, but as it stands count is not declared and not initialised. That could well be the real problem, but it is impossible to determine if that is a mistake when you edited the code or a mistake with the actual code. In fact, we have no idea if the problem exists in this code sample because you could not have compiled and run it... and for all we know, after fixing this code sample, the problem will continue to exist in your actual code because it is a different problem from what exists in the sample code.

    Even if you could show us the entire program, that would not be a good idea: chances are no one wants to carefully go over everything to find the problem. You need to narrow down the problem yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bandal27 View Post
    I have made a few minor syntax errors while trying to explain the problem to you guys....please over look it :-)
    But the problem is that if the code has no chance of compiling, WE KNOW that it is not the actual code that shows the problem. It is VERY COMMON that when code is posted that is a small section of code, the actual problem is not in the posted code. So overlooking typos and other errors in posted code is not a good thing - unless it's VERY obvious what is wrong, and why.

    Your example you have posted above is broken:
    in main instead of int main
    comma instead of semicolon after FILE *data_txt
    several missing end-braces.
    lcount and count not declared.

    functions trim and split are not declared or defined [so who knows what they do].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    27
    hi Laserlight

    I understand what you are saying but I know for a fact that the rest of the functions are working fine....
    Important thing is....do you see anything wrong at first glance in this piece of code..?

    Code:
          for(i=0; i < count; i++) {
     printf("%d %s %s", i,ptr->main[i].time, ptr->main[i].date);
          }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Other than you have an extra %s, no. We do not know what the types of all those things are, what they contain or if they are initialized or not.
    As laserlight says... take snippets of your code, put it in a main function, fix it up so it compiles, and if it still causes the problem, then post it here. That is posting the smallest possible compilable code that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. printing the contents the pointer points to
    By cblix in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 10:10 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM