Articles

Metasploit Framework Windows Tutorial
Remote Desktop Connection
Windows Processes That May Be Dangerous
How-To use NetCat a Tutorial
Common Linux Commands
Common Ports
Netcat Commands
HTTP Response Codes
War-Google Hack Terms
Wardriving
Avoiding Social Engineering and Phishing Attacks
Intrusion Detection on Linux
Linux Intrusion Detection
Penetration Testing Guide
Penetration Testing Tools
Social Engineering Fundamentals, Part I: Hacker Tactics
Social engineering (computer security)
The Psychology of Social Engineering

The Archives

General GSO
GovernmentSecurity.org News & Suggestions
In The News
Open Topic
General Security Information
Trash Can
Exploit & Vulnerability Mailing List Archives
Trial Member Forum
Product and Program Reviews GSO Tutorials
System Security
Windows Systems
Beginners Section
Linux & Unix Systems
File Downloads
Exploit Research & Discussion Trojan & Virus Errata
Networking Security / Firewall / IDS / VPN / Routers
System Hardening
E-Mail Security
Wifi Security
Trial Member Uploads
Upload discovered Trojans & Mal ware
GSO Programming Section
C , C++ , VC++
Visual Basic.NET
Perl /CGI
Java/Javascript
PHP/XML/ASP/HTML
Assembly + Other
The Cork Board
Network Security Consultant Directory
Network Security Jobs
The Archives
Encryption Information
General Network Security
Internet Anonymity
HTTP Protocol Security
Linux Security
MS IIS Information
Exploit Articles
Programming / Tool Design
GSO Software Projects
Public Downloads
Microsoft Security Questions and Papers

Full Version: C Programing Problem
SWilly
i've got this book "the full gueid to c lenguge" that i'm useing.
i've got to the point when u use structs that has a pointr inside them that points back to the same struct.

i came up with this code but i've got a compile error:

CODE

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct phoenum {
char first_name[7],last_name[12],address[22],tel[11];
       struct phoenum *nxt_phoen;
};

void init(struct phoenum *ptr);

//MAIN----------------------------------------------------

void main (void){

struct phoenum *first_phoen;

first_phoen = malloc (sizeof (struct phoenum));

if (first_phoen != NULL)
printf ("malloc ok! %p\n",first_phoen);
printf ("size in byte = %d\n",sizeof (struct phoenum));

init (first_phoen);
}
//End MAIN-------------------------------------------------

void init(struct phoenum *ptr){
ptr->first_name = "Bob";
ptr->tel = "089363106";
ptr->last_name = "Lipman";

printf ("Locations: %p ",ptr->tel);
printf (" %p ",ptr->first_name);
printf (" %p \n ",ptr->last_name);
printf ("last: %p:%s first: %s tel: %s\n",ptr->last_name,ptr->last_name,ptr->first_name,ptr->tel);
}

compile:c(28) : error C2106: '=' : left operand must be l-value.

inside the init function: "ptr->first_name = "Bob";" this cuses the error i've tried useing "*ptr->first_name = "Bob";" (*i belive it's wrong).. allthguo there isn't an error but when i try to print ptr->first_name value i get garbge.
skiddieleet
CODE

#include <stdio.h>
#include <stdlib.h>

struct phoenum {
char * first_name, * last_name, * address, * tel;
      struct phoenum *nxt_phoen;
};

void init(struct phoenum *ptr);

//MAIN----------------------------------------------------

int main (void){

  struct phoenum * first_phoen;

  first_phoen = malloc (sizeof (struct phoenum));

  if (first_phoen != NULL){
     printf ("malloc ok! %p\n",first_phoen);
     printf ("size in byte = %d\n",sizeof (struct phoenum));
  }else{
     fprintf(stderr, "error while mallocing\n");
     exit(-1);
  }

  init (first_phoen);
  free(first_phoen);
  return 0;
}
//End MAIN-------------------------------------------------

void init(struct phoenum *ptr){
  ptr->first_name = "Bob";
  ptr->tel = "089363106";
  ptr->last_name = "Lipman";

  printf ("Locations: %p ",ptr->tel);
  printf (" %p ",ptr->first_name);
  printf (" %p \n ",ptr->last_name);
  printf ("last: %s first: %s tel: %s\n",ptr->last_name,ptr->first_name,ptr->tel);
}


try that, or use strcpy to assign your strings to the char[]'s in the struct. I got different compiler errors. I get no errors with the above code. Good luck.
SWilly
Thanks works gr8 but i've got couple of Qs:
**U didn't change the init function... so the error wasn't there was it in the main program where ecxectly?

char * first_name, * last_name, * address, * tel; why are the chars pointers? can't they just be char as apart of the struct?

allso i'm not queit sure what free dose if it free the pointer thats no good i need the pointer to add structs (it's my "ancour").

"fprintf(stderr, "error while mallocing\n");" i had never seen this type of printf is stderr an error msg like sqlerror in php??

thanks again
skiddieleet
QUOTE
**U didn't change the init function... so the error wasn't there was it in the main program where ecxectly?
*


The error was where you were assigning the strings to the members of the struct. If you were to keep your struct as it was, you would have had to use strcpy to assign the strings to the members of your struct. Don't forget to include string.h for strcpy.
CODE

 strcpy(ptr->first_name, "Bob");
 strcpy(ptr->tel, "089363106");
 strcpy(ptr->last_name, "Lipman");

QUOTE
char * first_name, * last_name, * address, * tel; why are the chars pointers? can't they just be char as apart of the struct?

I changed them because of the compilers errors I was getting. You would have to do it like I have shown above (I think) for it to work how you made your struct. They are still a part of the struct though.
QUOTE
allso i'm not queit sure what free dose if it free the pointer thats no good i need the pointer to add structs (it's my "ancour").

free is to free the memory you malloced. I was going by the code you posted, and you didn't do anything else with it so I free'd it. You can use free wherever you want. Just be sure that you keep track of your mallocs and free them when you don't need that memory anymore. Don't reassign a pointer without freeing what it's pointing to unless it's not pointing to anything.
QUOTE
"fprintf(stderr, "error while mallocing\n");" i had never seen this type of printf is stderr an error msg like sqlerror in php??

fprintf is just like printf except that it allows you to give the FILE * which it uses for output rather than defaulting to stdout. Since it was an error message I used stderr.
remember there are man pages for all these functions on most any linux/unix box. Use them.

You said you were going from a book right? Isn't all this stuff in there? Sounds like you are making a linked list. Keep track of your pointers. Free unneeded ones. Don't hesitate to ask for more help but be sure to try and figure it out on your own first. Peace.
SWilly
Thanks, be sure that i'm trying to figuer out stuff on my own b4 i go and post here.
there are some stuff in the book there aren't explained at all (such as the free comand allthogu it covers pointers) ho and yjea i'm currently working with linked lists.
Thanks again

**at the moment i'm useing winxp, vc++ compiler so no man file i got my help file.**
ceder
When you don't know a fonction google it.
The first site you'll see will give you the prototype of the fonction, what it does and an explication to use the parameters.

Or search here :
hxxp://www.cplusplus.com/ref/
or in the msdn library :
hxxp://msdn.microsoft.com/library/

PS : Sorry for the bad english, I tried to do my best tongue.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.