help with char pointer array in C


 
Thread Tools Search this Thread
Top Forums Programming help with char pointer array in C
# 1  
Old 03-30-2011
help with char pointer array in C

i have an array like
Code:
    #define NUM 8
    ....
    new_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length);
    char *items[NUM] = {"one", "two", "three", "four", "five", "six", "seven", "eight"};
    char *item_name_length[NUM] = {"3", "3", "5", "4", "4", "3", "5", "5"};
    send_items(items, item_name_length, &NUM , &new_socket_fd);

Code:
void send_items(char *items[], char *item_name_length[], int *length, int *new_socket_fd) {
    int i, bytes;
    for(i = 0; i<*length; i++) {
        bytes = write(*new_socket_fd,items[i],atoi(item_name_length[i]));
        if (bytes < 0) error("ERROR writing to socket");
    }
}

and i get this error on the line where i call the function

error: lvalue required as unary â&â operand

whats wrong?
# 2  
Old 03-30-2011
NUM is not a variable. #define's do literal text substition at compile-time, which ends up trying to do send_items(items, item_name_length, &8 , &new_socket_fd);, which of course won't work.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

2. Programming

Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines? I have talked to one of the curators of the forum, but I am still not quite clear. Here... (1 Reply)
Discussion started by: yifangt
1 Replies

3. Programming

Unclear pointer and array

Hello, The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41. ./a.out thisisatest 0 8 substring = "thisisat"And my code is: #include <stdio.h> #include <stdlib.h> #include... (29 Replies)
Discussion started by: yifangt
29 Replies

4. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

5. Programming

C pointer/array duality confusion

Hi all, Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this... Say I declare a typedef to an array of pointers to some type... /** * An array of ptrs to sections */ typedef... (4 Replies)
Discussion started by: gorga
4 Replies

6. Programming

Array of char

I'm doing some coding in C++ Want to have a long empty string like below const char ModMisfit :: DelStr = "\r \r"; However due to the long blank the line is very long. Is there any way to avoid this and keep the... (5 Replies)
Discussion started by: kristinu
5 Replies

7. Programming

How to get the sizeof char pointer

The below code throws the error, since the size of x = 19 is not passed to the cstrCopy function. using namespace std; static void cstrCopy(char *x, const char*y); int main () { char x; const string y = "UNIX FORUM"; cstrCopy(x,y.c_str()); return 0; } void cstrCopy(char *x,... (3 Replies)
Discussion started by: SamRoj
3 Replies

8. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

9. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

10. Programming

Regarding char Pointer

Hi, char *s="yamaha"; cout<<s<<endl; int *p; int i=10; p=&i; cout<<p<<endl; 1) For the 1st "cout" we will get "yamaha" as output. That is we are getting "content of the address" for cout<<s. 2) But for integer "cout<<p" we are getting the "address only". Please clarify how we are... (2 Replies)
Discussion started by: sweta
2 Replies
Login or Register to Ask a Question