Thread: Array of pointer to characters

  1. #1

    Array of pointer to characters

    Hello,

    As a first note, I have done a lot of board searching on topics such as: "array of functions" and so forth, but can't find a definite solution.

    My question is this: is it possible to store 'pointer to characater's' inside an array of characters?

    For an example:
    Code:
    typedef void (*fnptr)(void);
    fnptr myFunc[3] = {func1, func2, func3};
    This allows someone to store an array of functions. Though, I wanted something similar for character arrays:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char* (char_set);
        
    
    int main() {
    	int i;
    	char *ptr, *pch, *p;
    	
    	char_set data[3] = {ptr, pch, p};
    
    	for (i = 0; i < sizeof(data)/sizeof(data[0]); i++) {
    		data[i] = malloc(10 * sizeof *data[i]);
    		if (data[i] == NULL)
    			return 0;
    		free(data[i]);
    		data[i] = NULL;
    	}
    	
    	return 0;
    }
    Even though it works with gcc, some other compilers I've tested it with seems to think otherwise. One compiler says:

    'initializer must be constant'

    GCC itself even says this if I turn -Wall and -pedantic on: 'warning: initializer element is not computable at load time'

    Is there any standard way I can accomplish this?


    - Stack Overflow
    Last edited by Stack Overflow; 03-28-2005 at 04:13 PM. Reason: Updated code
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you want 3 string each of 10 chars then maybe this would work for what you want.
    Code:
    char Pointer[3][10]

  3. #3
    Alright,

    I do see what you mean. And I just realized that what I was trying to do was a bit pointless.

    Here I wanted data[0] to point to ptr, data[1] to pch, and so forth. Thus I could allocate all of my seperate pointers a bit easier. I failed to see that I could just create a standard pointer to pointer to char and just use those instead.

    Ah, well I need to take a Vitamin C.

    Thanks for the help.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    #include <stdio.h>
    
    int main()
    {
      void *types[5];
      int a = 5;
      char c = 'a';
      float f = 1.5f;
    
      types[0] = &a;
      types[1] = &c;
      types[2] = &f;
    
      printf("%d\n\n", a);
    
      *((int *)types[0]) = 10;
    
      printf("%d, %c, %f\n",*((int *)types[0]),*((char *)types[1]),*((float *)types[2]));
    
      return 0;
    }
    I'm sure theres a reason not to do this. Plus it hardly seems useful.
    Last edited by nonpuz; 03-28-2005 at 05:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM