Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2005, 03:23 PM   #1 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Some questions about C

I learned how to program in C++, but all my courses this coming year are in C or Java (I'm pretty sure). I know my Data Structures course is going to be in C. So I've been focusing on learning more C this summer, and some things about C confuse me.

I'm finally understanding strings inside out, in any language. So I have a few things left to understand in C:
  • What the heck is a function pointer? Why would you use one?
  • Whats the purpose of a pointer to a pointer (such as char **t). I think it would be like, an array of pointers. For example I know how to manipulate char **argv, the same way I know how to manipulate char *argv[]. Is this why you use pointers to pointers? For manipulating arrays of pointers? Is there any other use?
  • I'm a little confused on extern variables, and more confused about extern functions if these even exist.

Theres others that I can't think of off the top of my head. I'll try to remember some of them and post here.
__________________
fp_unit is offline   Reply With Quote
Old 08-10-2005, 03:28 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
  • Function pointers see The C FAQ question 1.34, 4.12 and 20.6 gives a few pointers to where it can be used.
  • Considder this typical code:
    Code:
    #include <stdio.h> int main(){ int i; char * str_arr1[] = {"this is first", "this is second", NULL}; char **str_arr2 = {"this is first", "this is second", NULL}; for(i=0; str_arr1[i] != NULL; ++i) printf("str_arr1: %d : %s\n", i, str_arr1[i]); for(i=0; str_arr2[i] != NULL; ++i) printf("str_arr2: %d : %s\n", i, str_arr2[i]); return 0; }
    char *arg[] is almost the same as char** arg, only issue here is, that in the str_arr1 you explicitly told the compiler it's an array, thus you can assign the placeholders directly. With a char** you need, at runtime, to check for allocation of the needed memory in order to fill your array, since the compiler dosn't know what you're planing to do with it, it will issue a warning in this case, and your program will most likely give a segmentation fault.
  • extern only means you can access them througout your entire running code, which means you can define the exern variables in another file, the assign them a valu in another file and access them throughout every function and reassign them in another or read the value in a third function, take a look at this code
    Code:
    //baz.h extern int baz;
    Code:
    //test.c #include <stdio.h> #include "baz.h" int baz = 0; void foo(void); void bar(void); int main(){ int i; for(i=-1; i < baz; ++i){ if(baz > 3) bar(); else foo(); } } void foo(void){ printf("Calling foo(), baz = %d\n", baz); baz++; } void bar(void){ printf("Calling bar(), baz = %d\n", baz); baz--; }
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-14-2005, 02:50 PM   #3 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Thanks, that helped me a while ago when I read it, sorry for the late reply

Questions:

Can I do things like this with pointers to pointers? Why or why not?

Code:
/* somefile.h. */ #ifndef SOMEFILE__ #define SOMEFILE__ typedef struct { char **string_table; int num_strings; } string_table; int add_str_to_table(string_table*, const char*); #endif
Code:
/* somefile.c */ #include "somefile.h" int add_str_to_table(string_table * tbl, const char * str) { if((strcpy(tbl[0], str)) != 0) { perror("add_str_to_table"); return SOME_ERROR; } return SUCCESS; }
I meant for this to start as a small amount of code, after I finished I might as well copy/paste it into my compiler
__________________
fp_unit is offline   Reply With Quote
Old 08-14-2005, 06:22 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
Quote:
Can I do things like this with pointers to pointers?
The way you're using it there, No you cant.
The suggested function isn't valid.. And I think you know that too..
First off, you're using strcpy() which is provided in string.h, but you don't include it.
Second, you're accessing placeholder at index 0 of youre string_table struct pointer, this is an invalid assignment, since you crap on your orriginal deffinition of string_table
This is what I would do in that case:
Code:
/* somefile.h. */ #ifndef SOMEFILE__ #define SOMEFILE__ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char **string_table; int num_strings; } string_table; int add_str_to_table(string_table*, char*); #endif
Code:
/* somefile.c */ #include "somefile.h" int add_str_to_table(string_table* tbl, char* str) { tbl->string_table = (char**)realloc(tbl->string_table, (tbl->num_strings +1)*sizeof(char*)); if(!tbl->string_table){ printf("Not enough heap space to reallocate string_table\n"); return -1; } tbl->string_table[tbl->num_strings] = (char*)malloc(strlen(str)); if(!tbl->string_table[tbl->num_strings]){ printf("Not enough heap space to allocate added string\n"); return -2; } if(!strcpy(tbl->string_table[tbl->num_strings], str)){ printf("Unable to copy str to placeholder\n"); return -3; } tbl->num_strings++; return 0; } int main() { string_table * my_tbl; int i; char str1[] = "This is a string"; char str2[] = "This is a second string"; my_tbl = (string_table*)malloc(sizeof(string_table)); my_tbl->num_strings = 0; if(0 != add_str_to_table(my_tbl, str1)){ printf("Unable to add str1\n"); return -1; } if(0 != add_str_to_table(my_tbl, str2)){ printf("Unable to add str2\n"); return -1; } for(i=0; i < my_tbl->num_strings; ++i) printf("my_tbl->string_table[%d] = %s\n", i, my_tbl->string_table[i]); return 0; }
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 01:16 PM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
Starting out with C anon C 0 02-24-2003 01:06 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 11:58 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle