Passing a 2D pointer

This is a discussion on Passing a 2D pointer within the C Programming forums, part of the General Programming Boards category; Hello, I have run into a problem. I am trying to pass a two dimensional pointer and write data to ...

  1. #1
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Passing a 2D pointer

    Hello,

    I have run into a problem. I am trying to pass a two dimensional pointer and write data to it. The only thing is it always crashes if I write more than one array index. For example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void passtest(char ***string) {
    	// Two strings
    	*string = (char **)malloc(sizeof *string * 2);
    
    	if (*string) {
    		// Works great
    		(*string)[0] = (char *)malloc(sizeof *string[0] * 6);
    		strcpy(*string[0], "Hello");
    
    		// Crashes...
    		(*string)[1] = (char *)malloc(sizeof *string[1] * 3);
    		strcpy(*string[1], "Hi");
    	}
    }
    
    int main() {
    	int i;
    	char **test = NULL;
    
    	passtest(&test);
    
    	for (i = 0; i < 2; i++)
    		free(test[i]);
    	free(test);
    
    	return 0;
    }
    It may be something im doing wrong, or not allocating enough space. I don't know, thats why I'm here I've spent 30 minutes trying to figure it out and still have no clue.


    Thank you for your time,
    - 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.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First things first, remove the cast from the return of malloc()

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    You forgot some parentheses

    Code:
    void passtest(char ***string)
    {
        // Two strings
        *string = (char **) malloc(sizeof *string * 2);
    
        if (*string) {
            // Works great
            (*string)[0] = (char *) malloc(sizeof *string[0] * 6);
            strcpy((*string)[0], "Hello");
    
            // Crashes...
            (*string)[1] = (char *) malloc(sizeof *string[1] * 3);
            strcpy((*string)[1], "Hi");
        }
    }
    
    void passtest2(char ***string)
    {
        char **temp;
        temp = malloc(sizeof *temp * 2);
        if (temp) {
            temp[0] = malloc(sizeof *temp[0] * 6);
            strcpy(temp[0], "Hello");
            temp[1] = malloc(sizeof *temp[1] * 3);
            strcpy(temp[1], "Hi");
        }
        *string = temp;
    }
    I prefer the second form - use a temp variable of the correct type, then perform the assignment to the input pointer at the end. It removes one layer of indirection in the body of the code, making things just a little bit simpler.

    Oh, and watch the casts on the return result of malloc - bad idea in C - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Edit: Reading Salem's post now.
    Last edited by Stack Overflow; 08-05-2004 at 04:20 PM.
    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.

  5. #5
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Thanks Salem,

    It works now


    - 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM
  2. Passing a pointer to a 2D array
    By OakRand in forum C Programming
    Replies: 8
    Last Post: 05-18-2006, 12:50 AM
  3. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM
  4. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM
  5. Passing 2D arrays by reference
    By samGwilliam in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2002, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21