Thread: need with debaging - printing issue

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question need with debaging - printing issue

    Hi

    My code is an implemtation of phone book.
    I have 2 problems that I don't know/understand how to solve them.

    1 - the filed " phonebook_ptr->Name,name " is get empty when I'm
    getting out from the AddEntry(phone *phonebook_ptr) function.

    2 - the PrintEntry(phone *phonebook_ptr) function ,prints only the last
    entery at the AddEntry(phone *phonebook_ptr) function.

    thanks in advance
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    It's great to see well formatted code for a change :-)

    That global variable named 'counter' is a bad idea. Global variables are bad.

    Your problem is that you are always passing the same pointer to those functions, and they are using it directly, with -> instead of [i]. You always read/write in the same position.

    You should move the 'counter' variable to main(), and pass its value to the functions that need it.

    The functions that access the whole array will need its starting address and the number of elements that you actually stored (a copy of the 'counter' variable).

    The functions that access only one element need a bit less. You have two options there:

    a) Pass a direct pointer to the element that the function should access. The caller will do something like Print(Phones+i) or Print(&Phones[i]). The called function will receive it as a simple pointer and access the structure with ->

    b) Pass a pointer to the beginning of the array and the index of the element to be accessed. The caller will do Print(Phones,i). The called function will access the element with [i].

    Good luck :-)

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    Hi ,
    thanks for great help.
    I didn't change the print fuction but add "&phonebook_ptr->Suraname[x] " in the printf.
    I don't understand why I get the same problem...

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    It's rather: phonebook_ptr[x].Surname

    But you also have to fix the input function. It always writes in the first element of the array.

    Don't write expresions at random. There is a logic reason for writing a[x].n in one case but a.n[x] in another. You need to understand that logic.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    The expresion : (*phonebook_ptr)[x].Name - doesn't pass compilation

    I have the same problem with the input function and with the print function.

    I don't know how to implement a[x].n with pointers

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by ilans11il View Post
    The expresion : (*phonebook_ptr)[x].Name - doesn't pass compilation
    [..]
    Of course not. Remove that *

    Please read carefully:

    Quote Originally Posted by comocomocomo View Post
    It's rather: phonebook_ptr[x].Surname
    [..]
    Don't write expresions at random. There is a logic reason for writing a[x].n in one case but a.n[x] in another. You need to understand that logic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing issue
    By ilans11il in forum C Programming
    Replies: 3
    Last Post: 02-21-2013, 12:53 AM
  2. Strange issue with printing array value
    By CodeKate in forum C Programming
    Replies: 6
    Last Post: 11-24-2010, 04:52 PM
  3. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  4. Issue with printing a varying array
    By monki000 in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2010, 04:06 PM
  5. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM