|
 |
|
 |
 |
08-09-2005, 03:23 PM
|
#1 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
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.
__________________
|
|
|
08-10-2005, 03:28 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
- 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--;
}
|
|
|
08-14-2005, 02:50 PM
|
#3 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
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 
__________________
|
|
|
08-14-2005, 06:22 PM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
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;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:58 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|