i am trying to assing value to a structure member using pointer and then increment and print it .so i write the following code.not working though.how to fix this or how can i do something like this.help please

#include<stdio.h>
#include<stdlib.h>
typedef struct{
      int count;
      }number;

int main()
{
    number *p;
    p->count=0;
     for (p->count=0;p->count<5;(p->count)++){
             printf("%s",p->count)count;
             }
             getch();
}

Recommended Answers

All 10 Replies

number *p;'
p->count=0;

This is a classic error with pointers. A pointer MUST point to a valid object that you own. If the pointer is uninitialized, as in the two lines above, you CANNOT dereference it...period, full stop, no exceptions.

Once you initialize the pointer, all will be well, aside from the syntactic craziness of this line:

printf("%s",p->count)count;

Which should look like this:

/* Also note that I changed %s to %d because p->count is an int */
printf("%d", p->count);

A pointer MUST point to a valid object that you own

what do you mean by that???

  printf("%s",p->count)count;

sorry for the mistake.actually that happened during copy paste
i changed the code and its working

#include<stdio.h>
    #include<stdlib.h>
    typedef struct{
          int count;
          }number;

    int main()
    {
        number s;
        number *p;
         p=&s;
         p->count=0;
         for (p->count=0;p->count<5;(p->count)++){
               printf("%d",p->count);
                }
         getch();
    }

why

 p=&s;

necessary to access the members of structure?? why i cant get the members using the pointers directly like i did previously?

what do you mean by that???

Pointers point somewhere, right? Where do you think your pointer points? If the answer is "I don't know", it's not a valid pointer. It's fairly obvious that you don't understand the concept of pointers, so I'll direct you to your nearest beginner's book on C. They usually have a full chapter or two devoted to the topic.

thnx for your help.trying to avoid books and learn as much as i can from the web .books are boring ,you know that.i think u are right.i need detail study about pointers.thnx

trying to avoid books and learn as much as i can from the web

The problem with that is the quality on the web is hugely variable. You can find great stuff, but more often than not it's crap by people who know only a smidge more than you do.

Give this one a try.

books are boring ,you know that.

As an avid collector of books, I would disagree. ;)

what i meant by web is vedio tutorials and asking question in forums.actually that tutorials makes things easy to understand what was written in books.and asking questions to you guys always give me clues what i need to know.

what i meant by web is vedio tutorials and asking question in forums.

That doesn't refute my statement about quality. ;)

why
p=&s;
necessary to access the members of structure?? why i cant get the members using the pointers directly like i did previously?

when i asked you the question, i went through books but what i found is "i have to access members by poiner (p) using (s).there was a lot of examples but dont specify the answer why i need p=&s.then i asked you.books have answers but sometimes it is hard to find where the answer is.i like to read books.but it's annoying when i dont know where the answer is written.

there was a lot of examples but dont specify the answer why i need p=&s.

Because the pointer needs to point to an object. A pointer is nothing more than a variable that holds an address; if you don't give a pointer an address, it's completely useless. p = &s is the code that gives the pointer p the address of s. Then whenever you dereference p, you're actually accessing the object s.

That's why I suggested that you work on the concept of pointers, because if you understand the concept, the answer to your question is obvious. If you don't understand pointers, no amount of answers to the specific question will help until the overarching concept is understood.

i am working on it.thnx a lot for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.